Ad

How To Prevent An Endless Loop With Try/catch Exceptions JAVA

I am new to Java and it is my first time ever dealing with try and catch statements. I am working on this project for my intro class and have run into a problem. I need to validate user input using a do-while loop. Our specific instructions were to test our validation by entering "thirty" instead of 30 for the first question (height). Now that I have tried to add an exception, it is causing an endless loop. I am not sure how to solve this!

import java.util.Scanner;

public class Paint1 {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        double wallHeight = 0.0;
        double wallWidth = 0.0;
        double wallArea = 0.0;
        double gallonsPaintNeeded = 0.0;
        
        boolean validHeight = true;
        boolean validWidth = true;
        
        final double squareFeetPerGallons = 350.0;
        
        // Implement a do-while loop to ensure input is valid
        // Prompt user to input wall's height
        
        do {
            validHeight = true;
            try {
                System.out.println("Enter wall height (feet): ");
                wallHeight = scnr.nextDouble();
            
                if (wallHeight <= 0 ) {
                    throw new Exception("Invalid input, please enter a valid height.");
                }
            }
            catch (Exception excpt) {
                System.out.println(excpt.getMessage());
                validHeight = false;
            }
            
        }
        while (!validHeight);


        // Implement a do-while loop to ensure input is valid
        // Prompt user to input wall's width
        do {
            validWidth = true;
            try {
                System.out.println("Enter wall width (feet): ");
                wallWidth = scnr.nextDouble();   // ERROR 1: replaced wallHeight to wallWidth
            
                if (wallWidth <= 0 ) {
                    throw new Exception("Invalid input, please enter a valid width.");
                }
            }
            catch (Exception excpt) {
                System.out.println(excpt.getMessage());
                validWidth = false;
            }
            
        }
        while (!validWidth);

        // Calculate and output wall area
        wallArea = wallHeight * wallWidth;
        System.out.println("Wall area: " + wallArea +  " square feet");  // ERROR 2: separated the print statements and added the value of wallArea

        // Calculate and output the amount of paint (in gallons) needed to paint the wall
        gallonsPaintNeeded = wallArea/squareFeetPerGallons;
        System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");   // ERROR 3: fixed incorrect capitalization of gallonsPaintNeeded variable

    }
}
Ad

Answer

I have seen this questions few times before but I can't seem to find them and give you the link.

To fix this, you need to clear the buffer.

            catch (Exception excpt) {
                System.out.println(excpt.getMessage());
                validHeight = false;
                scnr.nextLine();  // clear the buffer.
            }
Ad
source: stackoverflow.com
Ad