I have an input read from scanner, which I am then comparing to a hard coded string variable
String MOVE = input.next(); // I type in "MOVE N"
MOVE = MOVE.trim();
System.out.print(MOVE.equals("MOVE N"));
This returned false. I added trim()
String MOVE = input.next(); // I type in "MOVE N"
System.out.print(MOVE.equals("MOVE N"));
Still false.
Any ideas? Thanks
You probably want nextLine()
. The next()
method will just return the next token , which by default will be the next word (ie it breaks at space).
Next time, you should add a call to System.out.println(MOVE);
to help you diagnose this - that way you could see that the result is just "MOVE"
instead of "MOVE N"
. (And then you'd consult the docs for Scanner.next()
...)
Additionally, I strongly recommend that you start following Java naming conventions for your variables - so move
instead of MOVE
for example.
String MOVE = input.nextLine (); // I type in "MOVE N"
MOVE = MOVE.trim ();
System.out.print (MOVE.equals ("MOVE N"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.