• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

exception handling help

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a project for my java class that has to do with exception handling. What i am trying to do is use a try catch block with multiple catches. when i run my program it just puts out an infinite loop. What it has to do is catch the InputMismatchException and then loop back to have the user retype two integers. I also have to define my own exception for dividing by zero...but that�s a question for later...please help...code below...thanks
----------------------------------------------------------------------------


EDIT by mw: Added Code Tags.
[ February 07, 2007: Message edited by: marc weber ]
 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow sorry about that...i had everything indented and everything...guess this forum didnt like it
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jason Mackie:
...when i run my program it just puts out an infinite loop. What it has to do is catch the InputMismatchException and then loop back to have the user retype two integers...


Welcome to JavaRanch!

When the InputMismatchException is thrown, the Scanner does not advance, so it's still trying to scan whatever input caused the exception. To advance the Scanner past the "bad" input, try adding the following as the first line of your catch block...

scan.nextLine();
 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
awesome thanks marc that worked...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic