• Post Reply Bookmark Topic Watch Topic
  • New Topic

can't figure out how to compare a int to a empty string to input an error  RSS feed

 
web Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! So I am having issues with the program below everything works but I can't figure out a way to add code that if a user just hits enter without inputting anything it says "sorry entering in nothing is not a valid choice" I am stuck on figuring out how to compare a int to a string any help would be awesome thanks!
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing the scanner delimiter to just a new line character if you want to be able to detect the user simply pressing the enter key.

     keyboard.useDelimiter("\n");
 
web Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So where would I place that? I should mention that "guess = news can.nextInt();" shouldn't be there and I don't know how to edit it out and I want to be able to throw an error and have them re enter a integer if they just hit enter without entering in anything
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put it just after where create the Scanner instance.
    Scanner keyboard = new Scanner(System.in);
    keyboard.useDelimiter("\n");

Then, read the input in as a String rather than int, and use something like Integer.valueOf() to get the integer value. Be sure to catch NumberFormatException for cases where the input does not represent an integer value - this would include your case where no input is provided.

The String value returned by Scanner.next() may include a "\r" or other white-space characters. If that is the case, use something like String.trim() to remove it before trying to evaluate the integer value.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you asked the scanner for an int, it will return you an int, or throw an exception.
So an empty string would be the exceptional case (i.e. handled by the catch statement).
Not sure if you can get a hold of the value that caused a problem at that point.

If you WANT to manually handle the strings, then you have to read it as a String (not a number) and convert it to a number later.
That will then let you explicitly check the string for null/empty.
Personally I like the scanner approach.


The code here looks a little confused. Are you wanting to read a number or a string?
I can see two different calls to scanners

guess = newScan.nextInt();
and then lower down:
keyboard.next();

Problems with this I can see:
- You are not USING the value of the call to keyboard.next().
- The setting of the guess variable is happening OUTSIDE of the loop. It can never change, so you will loop for ever!


 
web Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the correct version of the code the above one has a couple things wrong with it when I tried to run it

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:If you asked the scanner for an int, it will return you an int, or throw an exception.
So an empty string would be the exceptional case (i.e. handled by the catch statement).


I don't think the call to nextInt() will return if the input is just the enter key.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree.

Move your scanning calls out of that method. You will be using Scanners all the time so you will end up with lots of code duplication. Move them out of that class. Create a utility class with
  • 1: Private constructor, never called, because you don't need any instances of the class.
  • 2: Single static field for a Scanner pointing to System.in, because you only need one such Scanner object.
  • 3: Use a loop to reject anything which isn't an int.
  • You will find an example here. Rob Spoor taught me how to write that sort of loop. An empty String is not an issue like that; the Scanner will simply see it as part of its delimiter and will ignore it completely. If you push enter 1000000 times, your finger will drop off. But the Scanner will still be waiting for your guess number.

    I would disagree about changing the delimiter. The default delimiter on Scanner works nicely and changing to "\n" will probably cause all sorts of nasty Exceptions on a Window® box or an old Mac. The trim method may help, but I still think you are better off with the default delimiter.

    The call to myScanner.next(); is in fact necessary so as to find the next token and reject it. Close examination of WW's loop and comparison to Rob's will show they are actually based on the same logic.

    Note that any methods written following my suggestions will not be thread‑safe.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!