aspose file tools*
The moose likes Beginning Java and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Jeffery Miller
Greenhorn

Joined: Jan 16, 2008
Posts: 2
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


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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 ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
M Newhouse
Greenhorn

Joined: Dec 05, 2007
Posts: 3
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: 18671
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.


"I'm not back." - Bill Harding, Twister
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
Have a look at NumberFormatException.
M Newhouse
Greenhorn

Joined: Dec 05, 2007
Posts: 3
Thanks for the starting point. I'll take a look and see what I can find.
M Newhouse
Greenhorn

Joined: Dec 05, 2007
Posts: 3
The NumberFormatException (with the try/catch) worked great. Thanks for your help.
[ January 17, 2008: Message edited by: M Newhouse ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions