Results 1 to 4 of 4
  1. #1
    monkeynote is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Red face x input string is not equal to "x"

    Hello guys!

    im new to java but im having some problem with regards to getting input character to terminate the application.

    i want the user to press "x" to exit the application however, when i press "x" key, it does not exit. is there something wrong with my code? thanks in advance :)

    Java Code:
    import java.util.*;
    public class TestThreading {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args) {
    			String choice="";
    	    	do{
    	    		System.out.print("Press \"X\" to quit");
    	    		choice = console.next();
    	    	}while(choice!="x");
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: x input string is not equal to "x"

    Don't use the == and != operators if you want to compare Strings for (in)equality. Use the equals( ... ) method instead. Your textbook clearly explains why.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: x input string is not equal to "x"

    == and != compare the values of variables (actually expressions) to see if they are equal or not.

    If you want to compare instances of String or any other type, use their equals() method.

    Java Code:
    do {
        // whatever
    } while(!choice.equals("x"));
    Note that String also has an equalsIgnoreCase() which might be handy if you prompt them for "X", but then check for "x".

  4. #4
    monkeynote is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: x input string is not equal to "x"

    wow! that explains everything! coz' i've been banging my keyboard for few hours for this to work!

    thank you all guys for the help! :)

Similar Threads

  1. java.lang.NumberFormatException: For input string: ""
    By deshmukh.niraj04 in forum New To Java
    Replies: 16
    Last Post: 08-22-2011, 01:02 PM
  2. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  3. java.lang.NumberFormatException: For input string: ""
    By chathura992 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 01:10 AM
  4. string comparison with "=" and ".equal"
    By guavajuice in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 09:01 PM
  5. jsp insert into database error(java.lang.NumberFormatException: For input string: "")
    By cypher_girl in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-22-2009, 03:14 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
  •