1. Member
Join Date
May 2013
Posts
20
Rep Power
0

## 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:
Java Code:
```import java.util.Scanner;
import java.lang.Math;

// Quadratic formula is -b +/- root b^2 - 4ac divided by 2a

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 {
}
}

}```
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.
Last edited by Kartright; 05-30-2013 at 02:52 AM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,840
Rep Power
10

## Re: Error output

Java Code:
```boolean x1NaN = Double.isNaN(x1);
boolean x2NaN = Double.isNaN(x2);
if (x1NaN && x2NaN) {
} else if (x1NaN) {
} else if (x2NaN) {
}```
Regards,
Jim

3. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Error output

Thanks, jim!
Can you please tell me why it needs to be a boolean, though?

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,840
Rep Power
10

## 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
Java Code:
`if ( some expression using && or || )`
the stuff inside the parentheses evaluates to either true or false. So the following
are equivalent.

Java Code:
```if (a == b) {
// do something
}

// or
boolean result = a == b;
if (result) {
// do something
}```
Regards,
Jim

5. Member
Join Date
May 2013
Posts
20
Rep Power
0

Oh, alright.
Thanks!

6. Member
Join Date
May 2013
Posts
20
Rep Power
0

## Re: Error output

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
Java Code:
`if (Double.isNaN(x1) && Double.isNaN(x2))`
You simply assigned Double.isNaN(var) to a boolean.
Makes sense. Thanks, Jim!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•