7 Replies - 248 Views - Last Post: 13 January 2013 - 04:12 PM Rate Topic: -----

#1 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 137
  • Joined: 31-October 12

Problem with catching error with try and catch

Posted 12 January 2013 - 12:30 PM

my problem is the once i input any character into the program it goes into a infinite loop, i can only guess its something to do with the scanner method i am using but i dont know.

here the code.
public static int mainMenu()
  	{
  		Scanner in = new Scanner(System.in);
  		int input;
  		boolean inputMisMatch;
  		do
  		{
  			// set input mismatch to jump out of the loop
  			inputMisMatch = false;
  			input = 0;
  			
  			System.out.println();
  			System.out.println("*************************************************");
  			System.out.println("*** Main menu");
  			System.out.println("*** ");
  			System.out.println("*** 1. Proccess New Job (using decimal values)");
  			System.out.println("*** 2. Proccess new job (using RGB hexadecimal values)");
  			System.out.println("*** 3. Calibrate Dispenser");
  			System.out.println("*** 4. Dipsplay Summary of Jobs");
  			System.out.println("*** 0. Exit");
  			
  			try
  			{
  				System.out.print("***\n*** Please choose a option: ");
  				input = in.nextInt();
  			}
  			catch (Exception e)
  			{
  				System.out.println("\nPlease only type in intergers to provent form crashing");
  				
  				// set inputMisMatch to itirate though the loop again.
  				inputMisMatch = true;
  			}
  			System.out.println();
  		
  		} while (inputMisMatch );
  		//(input > 4)
  		
  		return input;
  	}




any help is appreciated
Thanks Dale

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with catching error with try and catch

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • Posts: 2,652
  • Joined: 29-July 11

Re: Problem with catching error with try and catch

Posted 12 January 2013 - 12:36 PM

You set input mismatch to true all the time. It will loop forever.
Was This Post Helpful? 0
  • +
  • -

#3 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 137
  • Joined: 31-October 12

Re: Problem with catching error with try and catch

Posted 12 January 2013 - 12:40 PM

yeah but at the start of the loop i set it to false again. on line 9?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: Problem with catching error with try and catch

Posted 12 January 2013 - 12:49 PM

View Postfarrell2k, on 12 January 2013 - 07:36 PM, said:

You set input mismatch to true all the time. It will loop forever.


No this is not it

The method will work fine if the user inputs an integer as his first input
When the user doesn't input an integer, your program will stop working

Your program doesn't work because nextInt() will keep reading the same illegal input over and over again
When an InputMismatchException is thrown, the Scanner doesn't consume the input, so it stays in the buffer

	  			catch (InputMismatchException e)
	  			{
	  				in.next(); //Consume the input
	  				System.out.println("\nPlease only type in intergers to provent form crashing");
	  				
	  				// set inputMisMatch to iterate though the loop again.
	  				inputMisMatch = true;
	  			}


Was This Post Helpful? 3
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • Posts: 2,652
  • Joined: 29-July 11

Re: Problem with catching error with try and catch

Posted 12 January 2013 - 01:03 PM

View PostDkSnowdon, on 12 January 2013 - 07:40 PM, said:

yeah but at the start of the loop i set it to false again. on line 9?


Sorry. I am on my mobile phone. I didn't see that it was being set to true inside that catch().. I thought it was outside of it. :oops:
Was This Post Helpful? 0
  • +
  • -

#6 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Problem with catching error with try and catch

Posted 13 January 2013 - 09:07 AM

Howdy;
I think that this line
(Exception e)


Has to have a specific Exception type, no?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Problem with catching error with try and catch

Posted 13 January 2013 - 11:26 AM

No

CasiOo has identified the problem
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: Problem with catching error with try and catch

Posted 13 January 2013 - 04:12 PM

View Postk3y, on 13 January 2013 - 11:07 AM, said:

Howdy;
I think that this line
(Exception e)


Has to have a specific Exception type, no?

Exception is the superclass of the specific Exceptions, like InputMismatchException. That catch block will catch any type of Exception.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1