Results 1 to 10 of 10
Thread: reading commands from console
- 11-15-2010, 05:03 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
reading commands from console
I'm trying to create a program which reads a command from console. If the command is "quit" then the program ends; otherwise, it does specific things.
The code I have is here:
However, when I run the program and type any of the commands (like "quit"), it doesn't work.Java Code:public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean unsavedWork = false; while (true) { System.out.println("Command:"); String userChoice = in.next(); if (userChoice == "quit") { if (unsavedWork) { // } break; } else if (userChoice == "load") { // } else if (userChoice == "help") { System.out.println("Blah Blah"); } else if (userChoice == "info") { // } else if (userChoice == "save") { // } else { System.out.println("Command " + userChoice + " is not supported."); } } System.out.print("Bye!"); }
Can you please help me?
-
Don't use == with Strings. Instead use the equals(...) or equalsIgnoreCase(...) method.
- 11-15-2010, 06:25 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Thank you! Can you explain why == doesn't work with strings in Java?
- 11-15-2010, 06:31 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-15-2010, 06:31 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Because the == operator uses memory locations to compare objects.
The above two strings are equal by content, but are stored at two memory locations, so the == operator would return false when comparing them.Java Code:String s1 = new String("abc"); String s2 = new String("abc");
EDIT: beaten by JosEver seen a dog chase its tail? Now that's an infinite loop.
- 11-15-2010, 09:21 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Don't use in.next() either if you want to read the whole command and not just the first character.
Have a look at the other next... methods of the Scanner class at Scanner (Java Platform SE 6)
Cheers,
ErikI'm new to Java but I like to help where ever I can. :)
- 11-16-2010, 10:11 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
So if == operator only compares references, why does it work for integers and other primary types?
int a = 2;
int b = 2;
a == b is true!
@venerik: Sure. This was only sample code to show the problem.
- 11-16-2010, 10:19 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Primary types do not have references.
I'm new to Java but I like to help where ever I can. :)
-
- 11-17-2010, 04:09 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
What can cause of java program to suddenly stop reading commands?
By ryuzog in forum New To JavaReplies: 25Last Post: 11-04-2010, 04:26 PM -
CMD is not reading commands
By colonial in forum Forum LobbyReplies: 1Last Post: 03-15-2010, 02:36 AM -
Help file for Java commands?
By tyang in forum New To JavaReplies: 2Last Post: 01-31-2010, 05:09 AM -
Low-level java commands
By artemff in forum New To JavaReplies: 8Last Post: 01-01-2010, 06:23 AM -
Reading a line from console using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks