Java Problems - Scanner Input and If (X == Input) Statements
I just read through seven threads related to Java Scanner issues, but none contained the answer to my problem.
I'm trying to get user input form the console. I create a Scanner object (input) then try to store the user's command in a string called "command." Then I pass "command" back to the original runGame function. It seems that Scanner/command do read the write text. (If I enter 'd' and then print command's contents it prints 'd'.)
But when I return the string and store it in nextMove, it breaks. For example, I'll enter 'd' but then (nextMove == 'd') if statement isn't called. In fact, no matter what I enter, only the else statement is entered.
What am I doing wrong? How can I fix it
Here's the original function code:
public void runGame() {
drawWindow();
while(true) {
String nextMove = takeInput();
if(nextMove == "d")
System.out.print("WORRRRDDD d");
else if(nextMove == "w")
System.out.print("WORRRRDDD w ");
else {
drawWindow();
System.out.print("YOU GOT ELSED");
continue;
}
}
}
And here's the takeInput/Scanner function code:
public String takeInput() {
String command;
input = new Scanner(System.in); // "input" is the Scanner object used for all input.
try{
System.o开发者_StackOverflow社区ut.print("Enter command (d, w, stop, restart, exit): ");
command = input.next();
} catch(Exception e) {
System.out.println("You entered an invalid command.");
return null;
}
System.out.println("\n RETURNING COMMAND" + command + " \n");
return command;
}
While you're helping (I'm going to search but if I don't find it) what's the best way to recognize blank input in a scanner object?
And should are there any other exceptions/catches I need to worry about for user input in a console?
Rather than using ==
, use .equals(String)
, so your check would look like:
if(nextMove.equals("d"))
==
checks that the variables are the exact same reference in memory, .equals()
checks that the two instances are logically equivalent.
As others have said, use .equals when checking for equality between Strings.
Furthermore, whenever you have a constant in your equality check you should put it first. e.g.
"abc".equals(someString);
This will only ever be a boolean. Whereas:
someString.equals("abc");
This can be a boolean or throw a NullPointerException.
you shouldnt be using == for string comparison , if i remember correctly use compare, compareTo or equals.
You shouldn't use "==" method to compare 2 String objects. You will need to use .equals() method.
Your changed part of code should look like :
if(nextMove.equals("d"))
System.out.print("WORRRRDDD d");
else if(nextMove.equals("w"))
System.out.print("WORRRRDDD w ");
Why ? referenced from here
The “==” operator compares two object references to see whether they refer to the same instance. It checks whether two objects are exactly the same object or not. Two strings may be different objects, but can have the same value (have exactly the same characters in them). In such case, “==” will return false.
Take special care when comparing objects. If you wish to see whether two objects are same or not, then use”==” to, or use equal() to see if they have the same value.
Also check following links:
http://www.zparacha.com/java-string-comparison/
http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml
http://www.javabeginner.com/learn-java/java-string-comparison
精彩评论