Try-Catch InputMismatchException

Cannot call user prompt...

Page 1 of 1

6 Replies - 10181 Views - Last Post: 23 October 2010 - 09:59 PM Rate Topic: -----

#1 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Try-Catch InputMismatchException

Posted 23 October 2010 - 09:00 PM

Ok, the user is supposed to enter a double, but if they accidentally enter a character or string they will be told to enter a numeric value. I want the program to re-prompt the user for input after the exception is thrown.

public static void main(String args[]) { // Begin main Method.
		System.out.print("Base of Right Triangle = "); // Print Input for Triangle.
		
		try {
			rightTriangleWidth = scan.nextDouble(); // Store User Input.
		} catch (InputMismatchException e) {
			System.out.println("Please Enter a numeric value: ");
		} finally { 
			rightTriangleWidth = scan.nextDouble();
		}

		System.out.print("\tSide of square = "); // Print Input for Square.
		squareSide = scan.nextInt(); // Store User Input.

		drawShapes(rightTriangleWidth, squareSide); // Draw a Right Triangle and Square with Dimensions..
	} // End main Method.


This post has been edited by x68zeppelin80x: 23 October 2010 - 09:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Try-Catch InputMismatchException

#2 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Try-Catch InputMismatchException

Posted 23 October 2010 - 09:09 PM

double rightTriangleWidth;
for(;;)/> {		
    try {
        rightTriangleWidth = scan.nextDouble(); // Store User Input.
        break;
    } catch (InputMismatchException e) {
	System.out.println("Please Enter a numeric value: ");
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Try-Catch InputMismatchException

Posted 23 October 2010 - 09:11 PM

Hey, don't mind me asking, but what is up with the "for( ;; ) loop"?

I am getting an infinite loop... -_-

This post has been edited by x68zeppelin80x: 23 October 2010 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Try-Catch InputMismatchException

Posted 23 October 2010 - 09:16 PM

It is an infinite loop that will loop for ever
The only way to exit it is with the break statement
You could also have coded it that way if you prefer
double rightTriangleWidth;
boolean validInput = false;  // assume not valid
while(!validInput) {		
    try {
        rightTriangleWidth = scan.nextDouble(); // Store User Input.
        validInput = true;    // make it valid and break the loop
    } catch (InputMismatchException e) {
	System.out.println("Please Enter a numeric value: ");
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Try-Catch InputMismatchException

Posted 23 October 2010 - 09:23 PM

View Postpbl, on 23 October 2010 - 08:16 PM, said:

It is an infinite loop that will loop for ever
The only way to exit it is with the break statement
You could also have coded it that way if you prefer
double rightTriangleWidth;
boolean validInput = false;  // assume not valid
while(!validInput) {		
    try {
        rightTriangleWidth = scan.nextDouble(); // Store User Input.
        validInput = true;    // make it valid and break the loop
    } catch (InputMismatchException e) {
	System.out.println("Please Enter a numeric value: ");
    }
}



I am sorry, but this code also gives me an infinite loop. I just want it to ask once. Please assist me. I have just written over 1000 lines of code and I just want to handle exceptions. :)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Try-Catch InputMismatchException

Posted 23 October 2010 - 09:40 PM

[quote name='x68zeppelin80x' date='23 October 2010 - 10:23 PM' timestamp='1287894207' post='1148432']

View Postpbl, on 23 October 2010 - 08:16 PM, said:

It is an infinite loop that will loop for ever
The only way to exit it is with the break statement
You could also have coded it that way if you prefer
double rightTriangleWidth;
boolean validInput = false;  // assume not valid
while(!validInput) {		
    try {
        rightTriangleWidth = scan.nextDouble(); // Store User Input.
        validInput = true;    // make it valid and break the loop
    } catch (InputMismatchException e) {
	System.out.println("Please Enter a numeric value: ");
    }
}


The only reason that will not work would be because the user never enter a valid double
and you will loop on the invalid input forever
Try that way
double rightTriangleWidth;
boolean validInput = false;  // assume not valid
while(!validInput) {		
    String line = scan.nextLine();   // read line valid or not
    System.out.println("User input: " + line);    
    try {    
        rightTriangleWidth = Double.parseDouble(line);
        validInput = true;    // make it valid and break the loop
    } catch (InputMismatchException e) {
	System.out.println("Please Enter a numeric value: ");
    }
}


or
double rightTriangleWidth;
boolean validInput = false;  // assume not valid
while(!validInput) {		
    try {
        rightTriangleWidth = scan.nextDouble(); // Store User Input.
        validInput = true;    // make it valid and break the loop
    } catch (InputMismatchException e) {
	System.out.println("Please Enter a numeric value: ");
        // flush garbage
        scan.nextLine();
    }
}


This post has been edited by pbl: 23 October 2010 - 09:42 PM

Was This Post Helpful? 1
  • +
  • -

#7 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Try-Catch InputMismatchException

Posted 23 October 2010 - 09:59 PM

[quote name='pbl' date='23 October 2010 - 08:40 PM' timestamp='1287895232' post='1148445']

View Postx68zeppelin80x, on 23 October 2010 - 10:23 PM, said:

View Postpbl, on 23 October 2010 - 08:16 PM, said:

It is an infinite loop that will loop for ever
The only way to exit it is with the break statement
You could also have coded it that way if you prefer

double rightTriangleWidth;
int squareSide;
boolean validInput = false;  // assume not valid
while(!validInput) {		
    try {
        rightTriangleWidth = scan.nextDouble(); // Store User Input.
        validInput = true;    // make it valid and break the loop
    } catch (InputMismatchException e) {
	System.out.println("Please Enter a numeric value: ");
        // flush garbage
        scan.nextLine();
    }
}



Thanks! That worked :) +1 Rep.


double rightTriangleWidth;
boolean validInput = false;  // assume not valid

public static void main(String args[]) throws InputMismatchException{ // Begin main Method.
		System.out.println("Please Enter Whole-Numbers Only:"); // Rules for User.
		System.out.print("Base of Right Triangle = "); // Print Input for Triangle.
		while(!validInput) { // Begin While-Loop.	
		    try { // Test Input.
		        rightTriangleWidth = scan.nextDouble(); // Store User Input.
		        validInput = true; // Validator.
		    } catch (InputMismatchException e) { // Catch Exception.
			System.out.print("Please Enter a numeric value: ");
		        scan.nextLine(); // Flush Garbage.
		    } // End Try-Catch.
		} // End While-Loop.

		System.out.print("Side of square = "); // Print Input for Square.
		validInput = false;		
		while(!validInput) {	// Begin-While Loop.
		    try { // Test Input.
		        squareSide = scan.nextInt(); // Store User Input.
		        validInput = true; // Validator.
		    } catch (InputMismatchException e) { // Catch Exception.
			System.out.print("Please Enter a numeric value: ");
		        scan.nextLine();  // Flush Garbage.
		    } // End Try-Catch .
		} // End While-Loop.
		drawShapes(rightTriangleWidth, squareSide); // Draw a Right Triangle and Square with Dimensions..
	} // End main Method.


This post has been edited by x68zeppelin80x: 23 October 2010 - 10:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1