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