'How do I make the code repeat after the exception is executed?
I know this particular exercise has been posted before but none of the answers I find do what I want this to do. I want the code to throw the exception one time if a string is entered such as "twenty". Then I want the code to repeat without terminating. Currently, if "twenty" is entered I get (code below) but I want it to end after the first "Invalid Entry" and repeat.
Enter wall height (feet):
twenty
Invalid Entry
Enter wall width (feet):
Invalid Entry
Wall area: 0.0 square feet
Paint needed: 0.0 gallons
import java.util.Scanner;
import java.util.InputMismatchException;
public class Paint1 {
public static void main(String[] args)throws Exception {
Scanner scnr = new Scanner(System.in);
double wallHeight = 0.0;
double wallWidth = 0.0;
double wallArea = 0.0;
double gallonsPaintNeeded = 0.0;
final double squareFeetPerGallons = 350.0;
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's height
do{
try {
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
if (wallHeight < 0.0) {
throw new InputMismatchException();
}
}
catch (InputMismatchException ex){
System.out.println("Invalid Entry");
}
}while (wallHeight != 0.0);
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's width
do{
try {
System.out.println("Enter wall width (feet): ");
wallWidth = scnr.nextDouble();
if (wallWidth < 0.0) {
throw new InputMismatchException();
}
}
catch (InputMismatchException ex){
System.out.println("Invalid Entry");
}
}while (wallWidth != 0.0);
// Calculate and output wall area
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + wallArea + " square feet");
// Calculate and output the amount of paint (in gallons) needed to paint the wall
gallonsPaintNeeded = wallArea/squareFeetPerGallons;
System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
if (scnr != null) {
scnr.close();
}
}
}
Solution 1:[1]
You have initialized wallHeight to 0.0 . When you are giving input as "twenty" ,wallHeight is still assigned to 0.0 . You can try printing wallHeight in catch block. That's the reason while loop is breaking .
Note the changes :
while condition should be while (wallHeight == 0.0).
Scanner object has to be initialized everytime you want to take input .
do{
try {
System.out.println("Enter wall height (feet): ");
Scanner scnr = new Scanner(System.in);
wallHeight = scnr.nextDouble();
if (wallHeight < 0.0) {
throw new Exception();
}
}
catch (Exception ex){
System.out.println("Invalid Entry "+ wallHeight);
}
}while (wallHeight == 0.0);
Solution 2:[2]
You need to clear out the leftover carriage return in the scanner stream before asking for input again after an exception:
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's height
do{
try {
System.out.print("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
if (wallHeight < 0.0) {
throw new InputMismatchException();
}
}
catch (InputMismatchException ex){
System.out.println("Invalid Entry");
scnr.nextLine();
}
}while (wallHeight == 0.0);
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's width
do{
try {
System.out.print("Enter wall width (feet): ");
wallWidth = scnr.nextDouble();
if (wallWidth < 0.0) {
throw new InputMismatchException();
}
}
catch (InputMismatchException ex){
System.out.println("Invalid Entry");
scnr.nextLine();
}
}while (wallWidth == 0.0);
Note also that the while loop condition was changed to == instead of !=1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ammy |
| Solution 2 | Idle_Mind |
