• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at NumberFormatException.
 
M Newhouse
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the starting point. I'll take a look and see what I can find.
 
M Newhouse
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The NumberFormatException (with the try/catch) worked great. Thanks for your help.
[ January 17, 2008: Message edited by: M Newhouse ]
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic