Stop a Loop at input of Character

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 10
Reputation: opethcire316 is an unknown quantity at this point 
Solved Threads: 0
opethcire316 opethcire316 is offline Offline
Newbie Poster

Stop a Loop at input of Character

 
0
  #1
Sep 28th, 2009
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.

  1. DataAnalyzer
  2. 8 {
  3. 9 public static void main(String[] args)
  4. 10 {
  5. 11 Scanner in = new Scanner(System.in);
  6. 12 DataSet data = new DataSet();
  7. 13
  8. 14 boolean done = false;
  9. 15 while (!done)
  10. 16 {
  11. 17 System.out.print("Enter value, Q to quit: ");
  12. 18 String input = in.next();
  13. 19 if (input.equalsIgnoreCase("Q"))
  14. 20 done = true;
  15. 21 else
  16. 22 {
  17. 23 double x = Double.parseDouble(input);
  18. 24 data.add(x);25 }
  19. 26 }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,681
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Stop a Loop at input of Character

 
0
  #2
Sep 28th, 2009
  1. String input = in.next();
  2. if (input.equalsIgnoreCase("Q"))
  3. done = true;

when you get the input you can do:
  1. String input = in.next();
  2.  
  3. // will return the first character
  4. // also check the length of input (if input has length 0 you will get an error)
  5. char inp = input.charAt(0);
  6. // 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster

Re: Stop a Loop at input of Character

 
0
  #3
Sep 29th, 2009
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.
Last edited by cgeier; Sep 29th, 2009 at 1:04 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,610
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Stop a Loop at input of Character

 
0
  #4
Sep 29th, 2009
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.
Last edited by BestJewSinceJC; Sep 29th, 2009 at 1:38 am.
Out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: opethcire316 is an unknown quantity at this point 
Solved Threads: 0
opethcire316 opethcire316 is offline Offline
Newbie Poster

Re: Stop a Loop at input of Character

 
0
  #5
Sep 29th, 2009
Thank you everyone who responded. I see in the Character doc a method called isLetter(), I believe that will do what I want it to. Thank you again for taking the time to respond and help me.

ERic
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC