user3453211
user3453211

Reputation: 13

Comparing array strings in java to get boolean

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 :)

Upvotes: 0

Views: 110

Answers (4)

luiscosta
luiscosta

Reputation: 855

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;

}

Upvotes: 0

Sky
Sky

Reputation: 3360

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.

Upvotes: 0

aliteralmind
aliteralmind

Reputation: 20163

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.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

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;
}

Upvotes: 2

Related Questions