I am just plain stuck. I can not figue out why when I type a word to test a palindrome sometimes it comes back as true and other times as false. Thank you for any help.
import java.util.*;
public class ec1
{
static Scanner kb = new Scanner(System.in);
public static void main(String[] args)
{
String word = " ";
String s = " ";
char letter;
int i=0, j=0;
boolean isPal;
System.out.println("Enter a sentence to test for a palindrome (CTRL-Z to end): ");
while (kb.hasNext() )
{
s =kb.nextLine();
for (i=0; i < s.length(); i++) {
letter = s.charAt(i);
if(Character.isLetter(letter))
{
word += letter;
}
}
if(isPal(s))
System.out.println("That is a Palindrome");
else
System.out.println("That is not a Palindrome");
word = "";
System.out.println("Enter a sentence to test for palindrome (CTRL-Z to end): ");
}
}
public static boolean isPal(String s)
{
int len = s.length();
int i= 0, j = 0;
char ch1, ch;
j = len - 1;
boolean found = false;
for (i = 0; i < len; i++)
{
ch = s.charAt(i);
ch1 = s.charAt(j);
if(!Character.isLetter(ch) || !Character.isLetter(ch1) || ch != ch1){
j--;
return false;
}else{
}
} return true;
}
}