Hello Everyone,
I am taking my first
Java programming class, and I am stuck on an unexplained detail with the buffered reader input. I want to take an input from the keyboard, and then use it in an if statement. I can take the screen information and I can return it to the screen without a problem, but if I try to use it for an if statement it just fails. What did they forget to to tell me? Your help would be appreciated. Below is a snippet of the code that returns the offending answer.
import java.io.*;
public class Guessing{
public static void main(
String[] args) throws IOException{
//list of variables for user input
String playAnswer = "y", playAnswer2 = "y";
BufferedReader dataIn = new BufferedReader(newInputStreamReader(System.in));
while (playAnswer2 == "y"){
System.out.print("\tPlease enter yes to play a new game, or no to quit the program: ");
playAnswer = dataIn.readLine();
playAnswer2 = playAnswer.substring(0,1);
System.out.println(playAnswer);
System.out.println(playAnswer2);
if (playAnswer2 == "y"){
System.out.println("This is the playAnswer2 == y choice! " + playAnswer2 + " " + playAnswer );
}
else {
System.out.println("This is the playAnswer2 != y choice! " + playAnswer2 + " " + playAnswer );
}
} //end class while (playAnswer2 == "y")
}// end class public static void main(String[] args)
} // end class public class Guessing
This compiles just fine albeit with the wrong return. Any good thoughts would be appreciated. Thanks Bill Boyd