Results 1 to 10 of 10
  1. #1
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default 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:

    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!");
    	}
    However, when I run the program and type any of the commands (like "quit"), it doesn't work.

    Can you please help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thank you! Can you explain why == doesn't work with strings in Java?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by merik View Post
    Thank you! Can you explain why == doesn't work with strings in Java?
    The == operator compares references (pointers) for objects. Only if two references point to the same String this operator yields true; it doesn't check the character content of the Strings.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Because the == operator uses memory locations to compare objects.
    Java Code:
    String s1 = new String("abc");
    String s2 = new String("abc");
    The above two strings are equal by content, but are stored at two memory locations, so the == operator would return false when comparing them.
    EDIT: beaten by Jos
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  7. #7
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    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.

  8. #8
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Primary types do not have references.
    I'm new to Java but I like to help where ever I can. :)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by merik View Post
    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!
    And the tricky thing is == will usually work for Strings as Strings are stored in something called the String pool, so Strings with the same character content often reference the same object, but this is not guaranteed!

  10. #10
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Replies: 25
    Last Post: 11-04-2010, 04:26 PM
  2. CMD is not reading commands
    By colonial in forum Forum Lobby
    Replies: 1
    Last Post: 03-15-2010, 02:36 AM
  3. Help file for Java commands?
    By tyang in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 05:09 AM
  4. Low-level java commands
    By artemff in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 06:23 AM
  5. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •