Results 1 to 4 of 4
- 01-18-2012, 06:49 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
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"); } }
- 01-18-2012, 07:07 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-18-2012, 07:08 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
Note that String also has an equalsIgnoreCase() which might be handy if you prompt them for "X", but then check for "x".Java Code:do { // whatever } while(!choice.equals("x"));
- 01-18-2012, 07:45 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
java.lang.NumberFormatException: For input string: ""
By deshmukh.niraj04 in forum New To JavaReplies: 16Last Post: 08-22-2011, 01:02 PM -
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
java.lang.NumberFormatException: For input string: ""
By chathura992 in forum New To JavaReplies: 3Last Post: 01-08-2011, 01:10 AM -
string comparison with "=" and ".equal"
By guavajuice in forum New To JavaReplies: 9Last Post: 04-22-2010, 09:01 PM -
jsp insert into database error(java.lang.NumberFormatException: For input string: "")
By cypher_girl in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-22-2009, 03:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks