0

I am trying to write a program which checks to see if the first two and last two letters of a string are the same. For example, frontAgain("edited"); should return true. However the following program only works for 2 char strings like "ed". I thought this might be a object comparison problem so i tried using lines like:

splitstring[0].equals(splitstring[length - 2])
splitstring[0] == splitstring[length - 2]
splitstring[0].equalsIgnoreCase(splitstring[length - 2])

But once again my program only worked for strings like "ed". Any ideas? Here is my code:

public class Mainjazz {

public static boolean frontAgain(String str){

    int length = str.length(); //gets length of string for later use in if statement

    String[] splitstring = str.split(""); //splits string into an array

    if((splitstring[0].equals(splitstring[length - 2])) && (splitstring[1].equals(splitstring[length - 1]))){  //checks if first two letters = last two
        return true;
    }else{
        return false;

    }
}

public static void main(String[] args) {

    System.out.println(frontAgain("edited"));


}

}

EDIT: Problem solved, thanks :)

1
  • 3
    Why would it work for more than 2 characters when you only compare two characters? Commented Jul 3, 2014 at 1:47

4 Answers 4

2

Based on your algorithm (and the code you posted), I would use String.toCharArray() like so,

public static boolean frontAgain(String str) {
  if (str != null && str.length() > 1) {
    char[] chars = str.toCharArray();
    int len = chars.length;
    return chars[0] == chars[len - 2] && chars[1] == chars[len - 1];
  }
  return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If splitstring is equal to "abcdefgab", then

splitstring[0].equals(splitstring[length - 2])

is essentially equivalent to

'a'.equals('a')   

If you want to compare two characters, you need

int len = splitstring.length;
splitstring.substring(0, 2).equals(splitstring.substring(len - 2, len);

And it is recommended to make sure that the string is (non-null) at least two--and likely four--characters in length, before attempting the comparison.

Good luck.

2 Comments

Thanks for your help. i am confused about why 'a'.equals('a') doesn't work. that should return true right?
Correct. I understood your goal as comparing two characters against two characters. Not one.
0
public static boolean frontAgain(String str){
        String str1 = str.substring(0,2);
        String str2 = str.substring(str.length() - 2);
        if (str1.equals(str2)) {
            return true;
        }
        else
            return false;
}

It will be simpler to do it this way by getting the first 2 character and last 2 character of a String and then compare them up.

Comments

0

Hope this helps:

public boolean areTheSame(String mystring) {

        //if you want to compare ignoring the upper cases
        //mystring = mystring.toLowerCase();

        if(mystring.substring(0, 1).equals(
                         mystring.substring(mystring.length()-2, 
                                            mystring.length()-1))){
            return true;
        }
        else return false;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.