I have another questoin about a piece of code...I need to make this program loop to read integer values until the user enters 999 and NOT blow-up if they enter something dumb like "abc", "$1,000", or "13.25". Examples would help
We're not really here to write code for you. Try something. Show us you have made some effort. You'll get suggestions, tips, and guidance. but nobody should just post the solution for you.
So, where do you think the problem is? What can you try changing to fix it? [ January 16, 2008: Message edited by: Fred Rosenberger ]
Never ascribe to malice that which can be adequately explained by stupidity.
M Newhouse
Greenhorn
Joined: Dec 05, 2007
Posts: 3
posted
0
I am just starting in Java, and I have a similar problem. Mine reads a string which I convert into a double. But, if a non-numeric character is entered instead, I get an error when parsing into a double. Any help? Thanks!
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Well, sounds like you're both doing about the same thing. I would recommend you try to catch the exception. Have you seen examples of a try/catch block? That's what you would use here. Jeffry needs to loop until a valid number is entered, so there will be a loop of some sort too. (I can't tell for M Newhouse - what do you want to do instead of an error?) The loop must be positioned so that you stay inside the loop when an exception is thrown. Unfortunately you're also inside the loop when an exception isn't thrown, so the challenge may be to figure out how to get out of the loop when the number is valid. Let's see what you can come up with.