Results 1 to 6 of 6
Thread: Exceptions for multiple inputs
 10292014, 08:58 AM #1Member
 Join Date
 Jul 2014
 Posts
 12
 Rep Power
 0
Exceptions for multiple inputs
I'm writing a program that accepts three doubles from the user, and performs calculations on them. I want to be able to handle input mismatch exceptions on each individually (i.e., if the user enters a double for the first, then a letter for the second, I'd like to be able to keep the value entered for the first, and only require that the user try again with the second, then the third, etc.)... I tried putting each input instance into its own try/catch blocks, but it always goes back to #1 when an invalid input is entered. Right now, I have all three in one try/catch:
Java Code:package com.itse2317; import java.util.Scanner; import java.util.InputMismatchException; class Triangle { public static double side1 = 0.0, side2 = 0.0, side3 = 0.0; public static void main(String[] args) { double perimeter = 0.0, semi = 0.0, area = 0.0; String choice = "y"; int count = 0; Scanner input = new Scanner(System.in); System.out.println("Welcome to the triangle calculation program."); do { try { System.out.print("Please enter the length of side 1: "); side1 = input.nextDouble(); System.out.print("Please enter the length of side 2: "); side2 = input.nextDouble(); System.out.print("Please enter the length of side 3: "); side3 = input.nextDouble(); } catch(InputMismatchException e) { if(count < 4) { System.out.println("Not a valid entry."); input.nextLine(); count++; continue; } else { break; } } triangleType(); perimeter = perimeter(side1, side2, side3); semi = perimeter / 2; System.out.println("\nThe triangle's perimeter is: " + perimeter); area = area(semi, side1, side2, side3); System.out.println("The triangle's area is: " + area); input.nextLine(); System.out.print("Would you like to continue (Y/N)?: "); choice = input.next(); } while(choice.equalsIgnoreCase("y")); System.out.println("Goodbye."); } public static void triangleType() { if(side1 == side2 && side2 == side3) { System.out.println("The triangle is Equilateral since it has equal" + " length on all three sides."); } else if(side1 == side2  side2 == side3  side1 == side3) { System.out.println("The triangle is Isosceles since it has two " + "sides of equal length."); } else { System.out.println("The triangle is Scalene with no equal sides."); } } public static double perimeter(double a, double b, double c) { double perimeter = a + b + c; return perimeter; } public static double area(double semi, double a, double b, double c) { double area = Math.sqrt(semi*(semi  a) * (semi  b) * (semi  c)); return area; } }
 10292014, 09:31 AM #2Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 26
Re: Exceptions for multiple inputs
Break your code down a bit:
Java Code:do while "continue" { // Get the input // do calculation // prompt for continue }
That method would:
Java Code:for each side { do while invalid input { // Prompt for input. // On exception then loop again // On valid value then exit. } } return the array
Please do not ask for code as refusal often offends.
** This space for rent **
 10292014, 10:39 AM #3Member
 Join Date
 Jul 2014
 Posts
 12
 Rep Power
 0
Re: Exceptions for multiple inputs
I guess I don't understand the 'for each' syntax. I tried looking it up, but the only example I found was printing 'i'... Here's what I tried:
Java Code:static double[] getNumber() { double[] sides = new double[3]; for(double i : sides) { try { System.out.println("Please enter the length: "); i = input.nextDouble(); } catch(InputMismatchException e) { System.out.println("Invalid input entered."); } } System.out.println("Side1: " + sides[0]); System.out.println("Side2: " + sides[1]); System.out.println("Side3: " + sides[2]); return sides; }
 10292014, 11:37 AM #4Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 26
Re: Exceptions for multiple inputs
That was pseudo code.
It didn't actually mean use foreach.
Sorry.
Java Code:for (int i = 0; i < <your array>/length; i++) { // Get a valid value for the double at index i. }
Please do not ask for code as refusal often offends.
** This space for rent **
 10292014, 01:04 PM #5Member
 Join Date
 Jul 2014
 Posts
 12
 Rep Power
 0
Re: Exceptions for multiple inputs
Okay, I think I'm getting closer... Here's my full code:
Java Code:package com.itse2317; import java.util.Scanner; import java.util.InputMismatchException; class Triangle { public static double[] sides = new double[3]; public static Scanner input = new Scanner(System.in); public static void main(String[] args) { double perimeter = 0.0, semi = 0.0, area = 0.0; String choice = "y"; System.out.println("Welcome to the triangle calculation program."); do { sides = getNumber(); System.out.println(sides[0]); System.out.println(sides[1]); System.out.println(sides[2]); triangleType(); perimeter = perimeter(sides[0], sides[1], sides[2]); semi = perimeter / 2; System.out.println("\nThe triangle's perimeter is: " + perimeter); area = area(semi, sides[0], sides[1], sides[2]); System.out.println("The triangle's area is: " + area); input.nextLine(); System.out.print("Would you like to continue (Y/N)?: "); choice = input.next(); } while(choice.equalsIgnoreCase("y")); System.out.println("Goodbye."); } public static void triangleType() { if(sides[0] == sides[1] && sides[1] == sides[2]) { System.out.println("The triangle is Equilateral since it has equal" + " length on all three sides."); } else if(sides[0] == sides[1]  sides[1] == sides[2]  sides[0] == sides[2]) { System.out.println("The triangle is Isosceles since it has two " + "sides of equal length."); } else { System.out.println("The triangle is Scalene with no equal sides."); } } public static double perimeter(double a, double b, double c) { double perimeter = a + b + c; return perimeter; } public static double area(double semi, double a, double b, double c) { double area = Math.sqrt(semi*(semi  a) * (semi  b) * (semi  c)); return area; } static double[] getNumber() { double[] sides = new double[3]; for(int i = 0; i < sides.length; i++) { do { try { System.out.print("Please enter the length: "); sides[i] = input.nextDouble(); input.nextLine(); } catch(InputMismatchException e) { System.out.println("Invalid input entered."); } } while(!input.hasNextDouble()); } System.out.println("Side1: " + sides[0]); System.out.println("Side2: " + sides[1]); System.out.println("Side3: " + sides[2]); return sides; } }
The next thing is that once an invalid entry is made, it enters into an infinite loop, printing the "invalid input" message over and over... I would have thought a 'continue' at the end of the catch block would fix this, but apparently not...
I appreciate all the help so far.
 10292014, 03:02 PM #6Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 26
Re: Exceptions for multiple inputs
Java Code:input.nextLine();
That's why you're having to hit return again.
And you need a boolean flag here instead:
Java Code:while(!input.hasNextDouble());
Java Code:boolean valid = false; do { // set valid true if you successfuly read a number } while (!valid);
Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads

Take multiple inputs in a single line.
By kunalgaurav18 in forum New To JavaReplies: 13Last Post: 10272012, 05:36 AM 
How to store multiple user inputs  should be a simple answer but I am lost.
By doowstados in forum New To JavaReplies: 1Last Post: 03192012, 01:11 AM 
Input class can't handle multiple inputs
By musdem in forum New To JavaReplies: 2Last Post: 03172012, 12:03 AM 
Multiple Exceptions with Array
By Cutter in forum New To JavaReplies: 1Last Post: 12112009, 07:02 PM 
if statement with multiple inputs?
By soc86 in forum New To JavaReplies: 3Last Post: 01202009, 04:44 AM
Bookmarks