When I put scan.nextLine in a while loop, it becomes a space the second time I go through it. I want it to read a quote even if I have a space but the side effect is that it doesn't read in what the user types in the second time if it's in a while loop. Can someone tell me how to fix this problem?
// *******************************************
// Palindrome.java
//
// Author: Andrew Date: 10/21/12
// *******************************************
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String again = ("Yes");
while (again.equalsIgnoreCase("Yes"))
{
int x = 0;
System.out.println("Please enter in a phrase.");
String phrase = scan.nextLine();
String phrase1 = phrase.toLowerCase();
while ((x<(phrase1.length()/2))&&
(phrase1.charAt(x)==
phrase1.charAt(phrase1.length()-1-x)))
{
x++;
}
if (x>=(phrase1.length()/2))
System.out.println("The phrase " + phrase
+ " is a palindrome.");
else
System.out.println("The phrase " + phrase +
" is not a palindrome.");
System.out.println("Please enter in Yes to " +
"test another phrase. " +
"Enter in No to quit the "
+ "palindrome test.");
again = scan.next();
}
}
}
Welcome to DrJava.
> java Palindrome
Please enter in a phrase.
[Stanley Yelnats]
The phrase Stanley Yelnats is a palindrome.
Please enter in Yes to test another phrase. Enter in No to quit the palindrome test.
[Yes]
Please enter in a phrase.
The phrase is a palindrome.
Please enter in Yes to test another phrase. Enter in No to quit the palindrome test.
[DrJava Input Box]