| Author |
Preventing program from crashing when user enters a char instead of a numerical input
|
Jay Dobrzyn
Greenhorn
Joined: Mar 19, 2004
Posts: 1
|
|
Hi! Am very new at java (only been doing if, while and so on. No idea of arrays, bufferstrings etc! Presently using consolewindow for input) and my problem is this: Basically, when I ask the user for a int or a double input and he/she enters a char, then the whole thing hangs; ideally, if the user does that, I'd like the question to be repeated. eg: c.out.println("Please enter number needed") ; num = c.input.readInt () ; I've been able to block sort the other way round, in that the program will loop back to the question should the user enter an int instead of a char by: if (!(word == 'h') || word == 'r') and then putting another c.out.println and readString etc underneath. For my problem I've tried doing whiles and ifs based on (!(num >= 0) %% num <= 0) but within a while statement, all I get is a just a mess! Is there a simple way to solve this using whiles and ifs? Thanks! Robin [ March 19, 2004: Message edited by: Jay Dobrzyn ]
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Look into try & catch with something like: If try/catch is new to you, look up exceptions in a good book. Bruce Eckell's Thinking In Java is free online: http://64.78.49.204/
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: Preventing program from crashing when user enters a char instead of a numerical input
|
|
|