0

I am having a problem on finding out how to show the "invalid input Good bye" message if someone for example inputs anything other than a number (e.g. "*" / "xyz") for the value of a,b,c in the quadratic calculator that I made

    DecimalFormat df = new DecimalFormat("0.00#");
    Scanner sc = new Scanner(System.in);

    Double a,b,c;
    System.out.println("Welcome to the Grand Quadratic Calculator ! ");
    System.out.print("Enter value of a = ");
    a = sc.nextDouble();
    System.out.print("Enter value of b = ");
    b = sc.nextDouble();
    System.out.print("Enter value of c = ");
    c = sc.nextDouble();
    double xone = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;
    double xtwo = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a;

        if(b * b - 4 * a * c >= 0)
        {
            System.out.println("x1 = " + df.format(xone));
            System.out.print("x2 = " + df.format(xtwo));
        }

        else
            System.out.print("Invalid Input. \nGood Bye.");
2
  • What do you mean? you want to print that if the input isn't a number? Commented Nov 20, 2016 at 11:09
  • use hasNextDouble with if along with nextLine inside else to flush the unwanted input Commented Nov 20, 2016 at 11:10

2 Answers 2

1

When the user enters invalid input, sc.nextDouble() will throw InputMismatchException. That will crash your current program, the code printing the "Invalid Input" message will never be reached.

You can wrap your code in a try block, and catch this exception:

try {
    System.out.print("Enter value of a = ");
    a = sc.nextDouble();
    // ...
} catch (InputMismatchException e) {
    System.out.print("Invalid Input. \nGood Bye.");
    // return or exit with failure
}

The "Good Bye" message suggests you want to exit on invalid input.

If you don't actually want to exit, then you can wrap the user input part, and the try-catch block within a loop, with limited or unlimited retries.

Sign up to request clarification or add additional context in comments.

Comments

0

To guide against wrong input from the user, you should put both the input statements and all arithmetic operations into a try .... catch block.

Try the code below :

DecimalFormat df = new DecimalFormat("0.00#");
Scanner sc = new Scanner(System.in);

Double a,b,c;
System.out.println("Welcome to the Grand Quadratic Calculator ! ");
System.out.print("Enter value of a = ");

try { //this line will disallowing the program from terminating when the users inputs an invalid value

a = sc.nextDouble();
System.out.print("Enter value of b = ");
b = sc.nextDouble();
System.out.print("Enter value of c = ");
c = sc.nextDouble();
double xone = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;
double xtwo = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a;

    if(b * b - 4 * a * c >= 0)
    {
        System.out.println("x1 = " + df.format(xone));
        System.out.print("x2 = " + df.format(xtwo));
    }
}
catch(InputMismatchException err){
    System.out.println("Invalid Input. \nGood Bye.");
}

The else statement have been removed and the catch block will handle all that.

I hope that worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.