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.
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.
for one thing(your basic problem i guess)
if (sn<=500 && sn>5000)
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.
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.
i think my advice was a little helpful though.
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.
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:-
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.
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.