• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception handling help

 
Jason Mackie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
wow sorry about that...i had everything indented and everything...guess this forum didnt like it
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
awesome thanks marc that worked...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic