943,800 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1410
  • Java RSS
Sep 28th, 2009
0

Stop a Loop at input of Character

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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 }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
opethcire316 is offline Offline
10 posts
since Sep 2009
Sep 28th, 2009
0

Re: Stop a Loop at input of Character

Java Syntax (Toggle Plain Text)
  1. String input = in.next();
  2. if (input.equalsIgnoreCase("Q"))
  3. done = true;

when you get the input you can do:
Java Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Sep 29th, 2009
0

Re: Stop a Loop at input of Character

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.
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Sep 29th, 2009
0

Re: Stop a Loop at input of Character

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Sep 29th, 2009
0

Re: Stop a Loop at input of Character

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
opethcire316 is offline Offline
10 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JVM,JDK,JRE........
Next Thread in Java Forum Timeline: Displaying "Correct" or "Incorrect" after answering quiz question on html page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC