i have an assignment where i have to make a program that reads input and evaluates whether or not it is a palindrome until the user terminates. i am having issues with the while loop. it works like it should upon first input, but then when it re-enters the loop, it always says it is not a palindrome, even when it is.
Scanner in;
in = new Scanner (System.in);
String original = ""; //variable for user input
String original2 = ""; //variable to help make input case insensitive
String reverse = ""; //variable to compare word in reverse
int length; //variable used to
int i; //variable used in for loop
//String response = "";
System.out.println("Enter a word to check if it is a palindrome!");
original = in.nextLine();
while (!original.equals("n"))
{
length = original.length();
original2 = original; //assigning a new variable to hold the input, so as to not change it
original2 = original.toLowerCase(); //converting input to all lower case using the holder variable
for (i = length -1; i >= 0; i--) //for loop reversing the word to check for palindrome status
{
reverse = reverse + original2.charAt(i);
}
if (original2.equals(reverse)) //if statement determining output if palindrome is true or not
{
System.out.println(original + " is a palindrome");
}
else
{
System.out.println(original + " is not a palindrome");
}
System.out.println("Enter another word or enter 'n' to end"); //allowing to continue
original = in.nextLine();
}
System.out.println("thank you!");
in.close();
}
i initially thought that it was an issue with the buffer, but i no longer think that is an issue.
i will really appreciate any help at all