| | |
Stop a Loop at input of Character
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
Hello, I am new to this forum and need a little help.
I am taking an intro to Java Course and we are creating a project that reads in a set of user inputted numbers and stops when the user inputs any letter.
From a textbook example they have the code quit when Q is entered. However, I need the code to stop when any letter is entered.
If anyone could help me with this I would greatly appreciated it.
Thank you, code at bottom.
I am taking an intro to Java Course and we are creating a project that reads in a set of user inputted numbers and stops when the user inputs any letter.
From a textbook example they have the code quit when Q is entered. However, I need the code to stop when any letter is entered.
If anyone could help me with this I would greatly appreciated it.
Thank you, code at bottom.
Java Syntax (Toggle Plain Text)
DataAnalyzer 8 { 9 public static void main(String[] args) 10 { 11 Scanner in = new Scanner(System.in); 12 DataSet data = new DataSet(); 13 14 boolean done = false; 15 while (!done) 16 { 17 System.out.print("Enter value, Q to quit: "); 18 String input = in.next(); 19 if (input.equalsIgnoreCase("Q")) 20 done = true; 21 else 22 { 23 double x = Double.parseDouble(input); 24 data.add(x);25 } 26 }
Java Syntax (Toggle Plain Text)
String input = in.next(); if (input.equalsIgnoreCase("Q")) done = true;
when you get the input you can do:
Java Syntax (Toggle Plain Text)
String input = in.next(); // will return the first character // also check the length of input (if input has length 0 you will get an error) char inp = input.charAt(0); // check if inp is a character
If you check the API for class: Character it has a lot of "is..." methods that will help you check what you want.
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Oct 2008
Posts: 103
Reputation:
Solved Threads: 14
See the post #7 in the following article:
http://www.daniweb.com/forums/post99...tml#post999241 . The "isInteger" method, basically does this, but rather than exiting the program, it returns false. You could use it as is (call the "isInteger" method), and then if it returns false, you have encountered a non-integer and can exit your program.
http://www.daniweb.com/forums/post99...tml#post999241 . The "isInteger" method, basically does this, but rather than exiting the program, it returns false. You could use it as is (call the "isInteger" method), and then if it returns false, you have encountered a non-integer and can exit your program.
Last edited by cgeier; Sep 29th, 2009 at 1:04 am.
•
•
Join Date: Sep 2008
Posts: 1,610
Reputation:
Solved Threads: 205
Whenever you do projects like that you should consider all of your options: for example, is your input file guaranteed to input only integers or characters? If so, you could quit on any input that is not an integer, and this would be no different than quitting when you see a character. To do that you would use the Scanner class to try to parse an integer, and on failure, you'd exit your loop.
Anyway, if you follow up on what JavaAddict said and check out the character class, you will definitely find a method that does what you're looking for, which he is aware of (he didn't give you the method name because it's important to learn to read the docs). I *think* you could also use the Unicode character set and see if what is read in is between the range of values that would make it a char a-Z.
PS: I do not think JavaAddict was suggesting the isInteger method - the second poster is either talking about something unrelated or had a different understand of JavaAddict's post than I did. In any case check the methods out for yourself.
Anyway, if you follow up on what JavaAddict said and check out the character class, you will definitely find a method that does what you're looking for, which he is aware of (he didn't give you the method name because it's important to learn to read the docs). I *think* you could also use the Unicode character set and see if what is read in is between the range of values that would make it a char a-Z.
PS: I do not think JavaAddict was suggesting the isInteger method - the second poster is either talking about something unrelated or had a different understand of JavaAddict's post than I did. In any case check the methods out for yourself.
Last edited by BestJewSinceJC; Sep 29th, 2009 at 1:38 am.
Out.
![]() |
Similar Threads
- Ending a While loop using stop (Java)
- input a number, (-) to stop.... (C++)
- To stop a loop (Python)
- user input stops loop (C++)
- Syntax Error (input character) (ASP)
- Count number of occurrences of a character search it in a file (C++)
- C++ (C++)
- What's going on with character values in this loop? (C++)
Other Threads in the Java Forum
- Previous Thread: Huffman Coding
- Next Thread: Sending sms through web application
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor crashcourse database draw eclipse error event eventlistener exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jni jpanel jtree julia link linux list loop machine map method methods migrate mobile netbeans newbie nls oracle parsing physics plazmic print problem program programming project radio recursion scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing textfield threads time tree trolltech unlimited utility webservices windows






