Need a Try Catch for the Input Mismatch Exception. Not sure where it goes. Needs
ID: 665331 • Letter: N
Question
Need a Try Catch for the Input Mismatch Exception. Not sure where it goes. Needs to return error message to user if they enter a string or invalid
//package assignment4;
import java.util.Scanner;
import java.util.*;
import java.text.*;
public class Assignment4 {
// @param args the command line arguments
public static void main(String[] args) {
System.out.println("Welcome to the Area and Perimeter Calculator ");
Scanner sc = new Scanner(System.in);
double length, width, area, perimeter;
//area = Double.parseDouble(area);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get the input from the user
System.out.print("Enter length: ");
length = sc.nextDouble();
System.out.print("Enter width: ");
width = sc.nextDouble();
//calucate and display to user
area= width * length;
perimeter= 2 * width + 2 * length;
System.out.println("Area: " + area);
System.out.println("Perimeter: " + perimeter);
//ask user if they want to contine
System.out.print("Continue? (y/n): ");
choice = sc.next();
sc.nextLine(); // discard any other data entered on the line
System.out.println();
} // end of while
}
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
}
Explanation / Answer
import java.util.InputMismatchException;
import java.util.Scanner;
public class Except {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean bError = true;
int n1 = 0, n2 = 0, nQuotient = 0;
do {
try {
System.out.println("Enter first num: ");
n1 = input.nextInt();
System.out.println("Enter second num: ");
n2 = input.nextInt();
nQuotient = n1/n2;
bError = false;
}
catch (Exception e) {
System.out.println("Error!");
}
} while (bError);
System.out.printf("%d/%d = %d",n1,n2, nQuotient);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.