aspose file tools*
The moose likes Beginning Java and the fly likes Preventing program from crashing when user enters a char instead of a numerical input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Preventing program from crashing when user enters a char instead of a numerical input" Watch "Preventing program from crashing when user enters a char instead of a numerical input" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preventing program from crashing when user enters a char instead of a numerical input