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.");
hasNextDoublewithifalong withnextLineinsideelseto flush the unwanted input