Aryan D.
Aryan D.

Reputation: 39

Writing a method that needs to look at a string and should tell whether two letters are next to each other

I am doing a lab for school and these are the instructions: "Write a program to see if a string contains a specific letter next to another specific letter."

This is the sample data:

Sample Data to input:

chicken a b
frog f g
chicken c k
apluscompsci a s
apluscompsci a p
apluscompsci s c
apluscompsci c p

This is the sample output:

false
false
true
false
true
true
false

This is my code so far:

public class AB
{
    public static boolean check( String s, String a, String b)
    {
        int i;
        if(s.indexOf(a) == -1 || s.indexOf(b) == -1)
        {
            return false;
        }
        if(s.indexOf(a) != -1 || s.indexOf(b) != -1)
        {
            for(i = 0; i < s.length() - 1; i++)
            {
                if(s.substring(s.indexOf(a) - 1,s.indexOf(a)).equals(b) ||  s.substring(s.indexOf(a) + 1,s.indexOf(a)).equals(b) || s.substring(s.lastIndexOf(a) - 1,s.indexOf(a)).equals(b) || s.substring(s.lastIndexOf(a) + 1,s.indexOf(a)).equals(b));
                {
                    return true;
                }
            }  
        }
        return false;
    }
}

This is what my runner looks like:

public class AplusRunner
{
    public static void main( String args[] )
    {
        System.out.println( AB.check( "chicken", "a", "b" ) );
        //add more cases
        System.out.println( AB.check( "frog", "f", "g" ) );
        System.out.println( AB.check( "chicken", "c", "k" ) );
        System.out.println( AB.check( "apluscompsci", "a", "s" ) );
        System.out.println( AB.check( "apluscompsci", "a", "p" ) );
        System.out.println( AB.check( "apluscompsci", "s", "c" ) );
        System.out.println( AB.check( "apluscompsci", "c", "p" ) );
    }
}

For some reason, it keeps displaying that the first string sample is false(this is supposed to happen), but for the second string sample that the string index is out of range at -1 and I have no clue how to fix this. Thank you for any help!

Upvotes: 3

Views: 345

Answers (3)

Nicholas K
Nicholas K

Reputation: 15423

The String class already provides many utility methods to preform multiple operations, why not use them rather than re-invent the wheel? Simply change your method to

public static boolean check (String s, String a, String b) {      
   // check if combination of both strings (a+b) are present in 's' 
   return (s.contains(a+b)) ? true : false;

}

Upvotes: 3

Aravinda Meewalaarachchi
Aravinda Meewalaarachchi

Reputation: 2629

I feels that you have done an unnecessary validation checking and you can simplify it as follows. Sorry for not using the meaningful names for parameters. Try to re implement this with your perspective.

public class AB {
  public static boolean isConsecutive(String s, String a, String b) {
    return s.indexOf(a + b) != -1;
 }

 public static void main(String args[]) {
    System.out.println(AB.isConsecutive("chicken", "a", "b"));
    System.out.println(AB.isConsecutive("frog", "f", "g"));
    System.out.println(AB.isConsecutive("chicken", "c", "k"));
    System.out.println(AB.isConsecutive("apluscompsci", "a", "s"));
    System.out.println(AB.isConsecutive("apluscompsci", "a", "p"));
    System.out.println(AB.isConsecutive("apluscompsci", "s", "c"));
    System.out.println(AB.isConsecutive("apluscompsci", "c", "p"));

 }
}

Output :

false
false
true
false
true
true
false

Upvotes: 4

Anuja Barve
Anuja Barve

Reputation: 320

You can simply create a temporary String by combining the individual letters and check for their index. This code should work for you.

    import java.util.*;

public class Letters{
    private static boolean CheckConsecutiveLetters(String str, String l1, String l2)
    {
        int index_l1 = str.indexOf(l1);
        String temp = l1 + l2;
        System.out.println(temp);
        if(str.indexOf(temp) >= 0)
        {
            return true;
        }
        return false;
    }

    public static void main(String[] args)
    {
        boolean output = CheckConsecutiveLetters("chicken", "c", "k" );
        System.out.println(output);
    }
}

Upvotes: 2

Related Questions