Results 1 to 12 of 12
Thread: Comparing characters.
- 05-12-2011, 09:52 PM #1
Member
- Join Date
- May 2011
- Posts
- 32
- Rep Power
- 0
Comparing characters.
I am writing a code (a beginners code, i set myself targets for a simple program, and along the way i pick up all the extra details about java) and i want to be able to ask the user a yes or no answer and test that in a condition. Here is what i wrote (narrowed down to just the necessary code);
This compiles perfectly well, but when i enter the y or n (or anything for that matter) it just passes to the else statement. Always. So i tried using char and all its ways, that didnt compile right. Then using int, which compiled, but--Java Code:import java.util.Scanner; class choice { public static void main(String[] args) { System.out.println("(y/n):"); Scanner scan = new Scanner(System.in); String ch = scan.nextLine(); if (ch=="y") { System.out.println("you chose yes (y)"); } else if (ch=="n") { System.out.println("you chose no (n)"); } else { System.out.println("you didn't choose yes or no!"); } } }
This compiles correctly but throws up an exception (see below) when either n or y is selected:Java Code:import java.util.Scanner; class choice { public static void main(String[] args) { System.out.println("(y/n):"); Scanner scan = new Scanner(System.in); int ch = scan.nextInt(); if (ch=='y') { System.out.println("you chose yes (y)"); } else if (ch=='n') { System.out.println("you chose no (n)"); } else { System.out.println("you didn't choose yes or no"); } } }
am i comparing strings or characters incorrectly? (im using jdk on linux btw)Java Code:james@james-laptop:~/Desktop/coding/Jcoding/examples$ java choice (y/n): y Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:857) at java.util.Scanner.next(Scanner.java:1478) at java.util.Scanner.nextInt(Scanner.java:2108) at java.util.Scanner.nextInt(Scanner.java:2067) at choice.main(choice.java:7)
Thanks in advance.
- 05-12-2011, 10:01 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You compare objects(which a string is) with equals. Chars can be compared with ==.
Try
Java Code:char c = 'y'; System.out.println(c == 'y');
- 05-12-2011, 10:01 PM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
An InputMismatchException is thrown when the received input type does not match the desired input type. In this case you asked for an int, but inputted something that isn't an int.
- 05-12-2011, 10:12 PM #4
Member
- Join Date
- May 2011
- Posts
- 32
- Rep Power
- 0
ok, so we have ruled the bottom one out, there is still the matter of my original code, why wont it recognize my input if it is comparing a string to a string (supposedly)
- 05-12-2011, 10:14 PM #5
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 05-12-2011, 10:16 PM #6
Please post the line(s) of code you're talking about.the matter of my original code
- 05-12-2011, 10:18 PM #7
Member
- Join Date
- May 2011
- Posts
- 32
- Rep Power
- 0
what scanner is used with characters?
- 05-12-2011, 10:21 PM #8
Member
- Join Date
- May 2011
- Posts
- 32
- Rep Power
- 0
reply
new code, based on others suggestions, cant get character scanner to work:
Java Code:import java.util.Scanner; class choice { public static void main(String[] args) { System.out.println("(y/n):"); Scanner scan = new Scanner(System.in); char ch = scan.next(); if (ch=='y') { System.out.println("you chose yes (y)"); } else if (ch=='y') { System.out.println("you chose no (n)"); } else { System.out.println("rrrrrrrrrgggggg, i hate you"); } } }
- 05-12-2011, 10:22 PM #9
Member
- Join Date
- May 2011
- Posts
- 32
- Rep Power
- 0
wait, scanner doesn't take char
-
Scanner#next() and Scanner#nextLine() return String objects. You should be able to easily extract the first (and only) character in the String via the String charAt(0) method.
- 05-12-2011, 10:25 PM #11
Member
- Join Date
- May 2011
- Posts
- 32
- Rep Power
- 0
crude method
ok, i've done it, but slightly crude isn't it? oh well, works!
thank you allJava Code:import java.util.Scanner; class choice { public static void main(String[] args) { System.out.println("(y/n):"); Scanner scan = new Scanner(System.in); String ch1 = scan.nextLine(); char ch = ch1.charAt(0); //turns string into char if (ch=='y') { System.out.println("you chose yes (y)"); } else if (ch=='n') { System.out.println("you chose no (n)"); } else { System.out.println("rrrrrrrrrgggggg, i hate you"); } } }
-
Why "crude"?
The main suggestion I'd have would be to change your String to lower case so that the code works even if the user presses Y or N, fix your indentation and use of braces so that the code is better spread out and readable, and check to be sure that the String is one char long. If no char's entered, you'll get an exception if you try to convert the 1st char in the String to a char. Also it's a good habit to close resources when done with them, including the Scanner object.
Java Code:System.out.println("(y/n):"); Scanner scan = new Scanner(System.in); String ch1 = scan.nextLine().toLowerCase(); // so it works if they enter Y or N if (ch1.length() == 1) { char ch = ch1.charAt(0); // turns string into char if (ch == 'y') { System.out.println("you chose yes (y)"); } else if (ch == 'n') { System.out.println("you chose no (n)"); } else { System.out.println("rrrrrrrrrgggggg, i hate you"); } } else { // if String is not one char long // print out error message } scan.close(); // may want to do this in the finally block of a try/catch block
Similar Threads
-
comparing Graphs and Comparing Matrix
By jetnor in forum New To JavaReplies: 0Last Post: 03-27-2011, 01:40 AM -
Comparing a character with characters within a string
By Civie in forum New To JavaReplies: 1Last Post: 10-21-2010, 07:06 PM -
Comparing arrays
By mitty in forum New To JavaReplies: 8Last Post: 04-14-2010, 11:55 AM -
Comparing dates
By Java Tip in forum Java TipReplies: 0Last Post: 01-28-2008, 09:02 AM -
comparing
By Feng in forum New To JavaReplies: 2Last Post: 11-23-2007, 09:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks