-
Help with a repeat
I'm doing a pig Latin translator and everything works fine except for the while loop I'm using to repeat in the client program.
Code:
import java.util.Scanner;
public class TheClient
{
public static void main(String[] args)
{
Scanner theScan = new Scanner(System.in);
String repeat = "y";
String englishText;
while(repeat.equals("y"))
{
System.out.print("Please enter english text:");
englishText = theScan.nextLine();
PigLatin translation = new PigLatin(englishText);
System.out.println("Translation: " + translation);
System.out.print("Press y to repeat: ");
repeat = theScan.next();
}
}
}
-
A few things:
(1) It helps to post code that others can actually compile and run. In your case this is easy because the client is a separate class.
Code:
//PigLatin translation = new PigLatin(englishText);
String translation = "whatever";
(2) Use System.out.println() so you can see what the values of variables are. This gives you some insight into what the scanner methods are doing.
Code:
System.out.print("Please enter english text:");
englishText = theScan.nextLine();
System.out.println("englishText=|" + englishText + "|");
// ...
System.out.print("Press y to repeat: ");
repeat = theScan.next();
System.out.println("repeat=|" + repeat + "|");
Notice how the pipe symbols (|) are handy to mark off a string which might start or end with white space.
(3) Actually describe your problem. "everything works fine except" says nothing. It's the "except" that we are interested in. What does happen? Describe actual and intended program behaviour. Even better, use the System.out.println() suggestion and describe under what circumstances variables end up with values that you don't intend or expect.