i want to know if there is something missing.
if i will input “race car” it must show Palindrome and if i will input “string beans” it must show not a palindrome but when i run the code it has an error.
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(String.java:686)
at praktis.Palindrome.main(Palindrome.java:11)
public static void main (String args[]) {
String word = JOptionPane.showInputDialog("Enter a String:");
String finalword = word.replaceAll(" ","").toLowerCase();
for (int x = word.length(); x >= word.length()-1; x--) {
//this is my line 11 //
finalword.charAt(x);
}
if(word.equals(finalword)) {
JOptionPane.showMessageDialog(null, "Palindrome");
}
else {
JOptionPane.showMessageDialog(null, "Not a Palindrome");
}
}
for (x = word.length()-1; x >= 0; x--)EDIT:
String word = JOptionPane.showInputDialog("Enter a String:"); String finalword = ""; int x; for (x = word.length()-1; x >= 0; x--) { finalword = finalword + word.charAt(x); } if (word.equals(finalword)) { JOptionPane.showMessageDialog(null, "Palindrome"); } else { JOptionPane.showMessageDialog(null, "Not a Palindrome"); }