-1

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?

  • If the next word is not a `double` it won't be read so every other attempt will fail. You can use `input.nextLine()` to read the whole line. – Peter Lawrey Apr 03 '19 at 17:07
  • What do you define as an invalid double number? You will automatically throw that exception if you enter something that is not a number. Furthermore, you could also use ifs to manually throw it if you don't like the number. Ex) `if (a < 1) throw new InputMismatchException();`. Also, you may want to consider putting the two lines after the `catch` into your `try` block as well. – JamieT Apr 03 '19 at 17:09

2 Answers2

0

According to Java Docs Scanner throws InputMismatchException if the next value is not a valid double value, so you should not take care of it manually.

But if you want to display a message when an exception occurs, you should catch it with try-catch block and do what you want to do.

the equations don't register the variable a

This happens because a is part of the "try" block scope. Once you exit this block, the variable is gone.

What you probably want to do is:

 double a; //declaring variable a (main() method scope)
 try {
     a = input.nextDouble(); //assigning a new value
 }
 catch (InputMismatchException e) {
     System.out.println("That's not a valid number.");
     throw e; //notice, the exception is still thrown after the message is printed
 }

So now a is in the main method's scope and you can use it after the try-catch block.

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
0
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.

DasElias
  • 569
  • 8
  • 19
  • Thanks, I was using an older version of my code which I forgot to update so all of the errors that I made are gone except for the try/catch system. I understand that you can just put everything into the block! – Christopher Tan Apr 03 '19 at 17:41
  • I have a question, why use double a = Double.parseDouble(input.nextLine()); instead of double a = input.nextDouble(); ? Just wondering! – Christopher Tan Apr 03 '19 at 17:51
  • With `input.nextDouble()` it depends on your region if , or . is used as decimal point. (but you can change the `Locale`-object of the scanner or the comma delimiter), and `parseDouble()` uses . as decimal point always. You can use `.nextLine()` as well as `.nextDouble()`/`.nextShort()`, but you mustn't mix them. – DasElias Apr 03 '19 at 18:18