Search code examples
javacalculatorinputmismatchexception

How do I catch InputMismatchException for doubles a, b, and c in this code?


So I made a calculator that solves the quadratic formula, but I want the program to throw an exception when the variables a, b, or c aren't valid double numbers. However, I can't figure out how to get the variables into the equations that I want them to be in, so here is the code.

I don't know what background to put, I'm really new to java programming and I couldn't find answers to my specific problem anywhere else.

public static void main(String[] args) {

    Scanner input = new Scanner (System.in); //scanner

    short repeat = 1;
    while (repeat == 1) {

        System.out.println("Enter your equation by entering a, b, and c."); //introduction
        System.out.println("Press enter evey time you enter a number.");

        try {
            double a = input.nextDouble();
        }
        catch (InputMismatchException e) {
            System.out.println("That's not a valid number.");
        }

        double b = input.nextDouble();
        double c = input.nextDouble();

        double answer1 = ( (-b) + Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a); //answers
        double answer2 = ( (-b) - Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a);

        System.out.println("Your answers are: " + answer1 + " and " + answer2);

        System.out.println("Would you like to calculate more numbers? 1 for yes, 0 for no: ");
        repeat = input.nextShort();

    }

input.close();

}

I wanted the try/catch thing to work and for it to output a value that can be used in the equations, but the equations don't register the variable a. How would I go about doing the try/catch or any other method of displaying an error message?


Solution

  • public static void main(String[] args) {
        Scanner input = new Scanner (System.in); //scanner
    
        short repeat = 1;
        while (repeat == 1) {
    
    
            System.out.println("Enter your equation by entering a, b, and c."); //introduction
            System.out.println("Press enter evey time you enter a number.");
    
            try {
                double a = Double.parseDouble(input.nextLine());
                double b = Double.parseDouble(input.nextLine());
                double c = Double.parseDouble(input.nextLine());
    
                double answer1 = ( (-b) + Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a); //answers
                double answer2 = ( (-b) - Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a);
    
                System.out.println("Your answers are: " + answer1 + " and " + answer2);
            }
            catch (NumberFormatException e) {
                System.out.println("That's not a valid number.");
            }
    
    
    
            System.out.println("Would you like to calculate more numbers? 1 for yes, 0 for no: ");
            repeat = Short.parseShort(input.nextLine());
    
        }
        input.close();
    
    }
    
    • You have initalize the repeat variable to 1 and not to 0, since the program will not enter the while loop otherways.

    • You need to put your calculations for answer1 and answer2 in the try-catch-block too, because if the user enters an invalid value, the result shouldn't get calculated.

    • You should use only one Scanner instance, so you mustn't instance it in the loop.