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.

DataAnalyzer
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      DataSet data = new DataSet();

      boolean done = false;
      while (!done)
      {
         System.out.print("Enter value, Q to quit: ");
         String input = in.next();
         if (input.equalsIgnoreCase("Q"))
            done = true;
         else
         {
            double x = Double.parseDouble(input);
            data.add(x);
         }
      }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

String input = in.next();
     if (input.equalsIgnoreCase("Q"))
         done = true;

when you get the input you can do:

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.

See the post #7 in the following article:
http://www.daniweb.com/forums/post999241.html#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.

Edited 7 Years Ago by cgeier: n/a

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.

Edited 7 Years Ago by BestJewSinceJC: n/a

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

This article has been dead for over six months. Start a new discussion instead.