• Post Reply Bookmark Topic Watch Topic
  • New Topic

entering integers  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to get a user to enter an integer (registration number) between 500 and 5000. if it is below 500 and over 5000 the user should get a prompt "invalid number" and "non-numeric character"

please see my code below. it is not working when i enter invalid numbers, does my logic make sense. any help would be appreciated.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bad luck. You are going to use that sort of code hundreds of times. So don't write it. At least not there. Create yourself a utility class:-
All static members.
Private constructor, never called, because you never need any instances.
Private field, a Scanner pointing to System.in, which as you will see from this thread.
You use the hasNextInt() method to make sure what follows really is an int. Try here for more info.

Quick‑fix solution: you have got the wrong logical connective; i < 500 && i > 5000 is never going to work.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell gives good advice as usual. read what he said before you read this.
for one thing(your basic problem i guess)
if (sn<=500 && sn>5000)
is wrong.
it should be if (sn<=500 || sn>5000)
but that doesn't take into account non-numeric entries.
for that i usually use Integer.parseInt(String)
if the entry is not an int it will throw an exception that you can catch. i usually just catch it and present the dialog again. not the most robust i admit, but it works.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Randall

If you are using a Scanner, the technique Rob showed me ages ago, using hasNextInt(), ensure you never suffer an exception. In the original example of rob's, he took precautions against the Scanner being closed. That should not be a problem with a Scanner pointing to System.in, because you should never close it.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, can you believe that after 14 years of writing java i have never used a Scanner?
i think my advice was a little helpful though.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:Campbell, can you believe that after 14 years of writing java i have never used a Scanner?

If it makes you feel any better the Scanner class has only been available since Java 5 which was released in 2004 so you've only not used it for 10 years
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As someone who is new to the whole Java thing, I've used nothing but the scanner. I imagine that eventually I will find another method of getting user input.

For Carolin, you might want to look through <this>. It will, as Campbell has recommended, get you started on your utility class. It will also ease you into catching exceptions (capturing errors and dealing with them without crashing your program.)

I haven't used the .hasNextInt() method. Thus far I have been using the scanner's .nextLine() method -- treats all keyboard input as string data. From there, you can cast it to whatever you need.

Regards,

Robert
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:From there, you can cast it to whatever you need.

No you can't. However, you can parse the String and convert it to an int, float etc assuming it contains the correct characters ie those representing numeric values, decimal points etc.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall have to write my own Scanner FAQ

I believe Scanner is not designed to be used with nextLine all the time. It is designed to use nextXXX(), Did you find the loop which Rob suggests? That loop allows you to get the next whatever from the keyboard, and print an error message if the entry is not in the correct format. You will find that loop in one of the threads I linked to earlier. You will even find the beginnings of a utility class. I would suggest you change that to have a static field which is a Scanner pointing to System.in and use that Scanner throughout. Then you can get an int like this:-Challenges:-
  • 1: Enhance that class so you can get different types of input, e.g. BigDecimal, double.
  • 2: Overload its methods so you can accept only input in a certain range.
  • 3: (More difficult) Enhance it so you can get the next line from KeyboardInputs.nextLine()
  • 4: (Maybe for later on) Make the class thread‑safe.
  •  
    Robert D. Smith
    Ranch Hand
    Posts: 221
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I had some long winded reply for Tony and Campbell, but my computer decided that I really didn't want to post it so it trashed it.

    Tony, you are correct. I was unclear and used a poor choice of word(s). Where I had written cast I was implying one would then use the .parseXXX() methods as needed.

    Campbell, no, I was not aware that Scanner's behavior was such. I will be going through the Java Tutorials and docs later today. Also, I did not see the loop that Rob suggested (unless you meant the loop from the input handling link I posted). Anyway, double checking the links as well.

    thanks for the help and clarifications.

    Regards,
    Robert
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Robert D. Smith wrote:For Carolin, you might want to look through <this>. It will, as Campbell has recommended, get you started on your utility class. It will also ease you into catching exceptions (capturing errors and dealing with them without crashing your program.)

    @Carolin: But when you do, please remember that it's only one way of doing it. I wrote the page, and it reflects my attitude to the Scanner class which, as you can see, is not shared by everyone.

    It's probably also worth remembering (and the page tries to explain) that user input is fiddly, which is probably all the more reason for putting it in a utility class.

    HIH

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote: . . . all the more reason for putting it in a utility class.

    HIH

    Winston
    Yes, once you have got the utility class to work, you can continue to use it for ever.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!