# Error output

• 05-30-2013, 12:07 AM
Kartright
Error output
For extra credit I needed to make some sort of formula calculator. I went quadratic formula. Teacher told me it had to either print out an error or throw an ArithmeticException when any answers were NaN.
Here's my current code:
Code:

```import java.util.Scanner; import java.lang.Math; // Quadratic formula is -b +/- root b^2 - 4ac divided by 2a public class quadratic {         public static void main(String args[]){                 Scanner input = new Scanner(System.in);                 double a, b, c, x1, x2, top; //top represents the numerator                 System.out.println("Enter your a, b, and c values (in that order):");                 a = input.nextDouble();                 b = input.nextDouble();                 c = input.nextDouble();                 top = Math.sqrt((b*b) - (4*a*c));                 x1 = (-b + top) / (2*a);                 x2 = (-b - top) / (2*a);                 if (Double.isNaN(x1)){                         System.err.println("ERROR: First answer is not a real number!");                 } else if (Double.isNaN(x2)){                         System.err.println("ERROR: Second answer is not a real number!");                 } else {                         System.out.println("Your answers are: " +x1+ ", " +x2);                 }         } }```
I need to code it so that if x1 is NaN, an error is thrown (as I somewhat have done), and it displays the x2 answer (and vice versa). I also need to make it so that if both answers are NaN, an error is shown.

Currently all I can figure out is how to show a message when one of them is NaN, and how to show a message if neither are NaN. Any help would be greatly appreciated.
• 05-30-2013, 01:31 AM
jim829
Re: Error output
Code:

```boolean x1NaN = Double.isNaN(x1); boolean x2NaN = Double.isNaN(x2); if (x1NaN && x2NaN) {       // your message here } else if (x1NaN) {       // your message here } else if (x2NaN) {     // your message here }```
Regards,
Jim
• 05-30-2013, 01:42 AM
Kartright
Re: Error output
Thanks, jim!
Can you please tell me why it needs to be a boolean, though?
• 05-30-2013, 02:29 AM
jim829
Re: Error output
Because the Double.isNaN() method returns a boolean.
I just assigned the return values to boolean variables
and used those instead of re-invoking the methods. Sometimes it
looks cleaner.

Whenever you use a logical expression like
Code:

`if ( some expression using && or || )`
the stuff inside the parentheses evaluates to either true or false. So the following
are equivalent.

Code:

```if (a == b) {   // do something } // or boolean result = a == b; if (result) {   // do something }```
Regards,
Jim
• 05-30-2013, 02:44 AM
Kartright
Re: Error output
Oh, alright.
Thanks!
• 05-30-2013, 05:28 AM
Kartright
Re: Error output
Quote:

Originally Posted by jim829
Because the Double.isNaN() method returns a boolean.
I just assigned the return values to boolean variables
and used those instead of re-invoking the methods. Sometimes it
looks cleaner.

After revising my code, I re-read what you said and figured out what you meant by cleaner.

Rather than type
Code:

`if (Double.isNaN(x1) && Double.isNaN(x2))`
You simply assigned Double.isNaN(var) to a boolean.
Makes sense. Thanks, Jim!