Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: How to handle quadratic roots that don't exist?

1. ## How to handle quadratic roots that don't exist?

Hi guys, I'm a new Computer Science major and taking a Java class this semester. I'm unsure about 1 part of an assignment I've been working on. I need to display a few strings in the event that the roots to a quadratic equation do not exist. I'm wondering how to go about doing this.

From what a know if a value doesn't exist the program would return a value of "NaN", is that correct? However in the NetBeans IDE I'm not able to set and if statement such as:

```if(n > 0){

double discriminant;
double root1;
double root2;

discriminant = Math.sqrt(b*b-4*a*c);
root1 = (-b + discriminant)/(2*a);
root2 = (-b - discriminant)/(2*a);

PrintWriter out = new PrintWriter(new FileWriter("results.dat", true));

out.println("Root1: " + root1 + ", Root2: " + root2);
out.close();

if(root1 = NaN, root2 = NaN) {
System.out.println("Quadratic equation with the following coefficients:");
System.out.println("a: " + a + ";" + " b: " + b + ";" + " c: " + c + ";");
System.out.println("has the following roots: " + root1 + " and " + root2);
}
}```

As you can see, what I'm wondering how to accomplish is the nested If statement down at the bottom. Int "n" is a number entered into the command line when running the App. Variables a, b, c are doubles asked from the user.

One other part I'm confused on is how to specify one parameter or the other in the IF statement, as I wrote:

if(root1 = NaN, root2 = NaN)

Thanks for the help!

EDIT: The strings we're copied from a previous assignment and we'll be changed later to display different information, such as "The quadratic equation has non-real roots." or something along those lines. Sorry for the confusion.

2. ## Re: How to handle quadratic roots that don't exist?

Got some help with this issue elsewhere, just going to post my findings in case anyone brings this thread up in a search:

```if(n > 0){

double discriminant;
double root1;
double root2;

discriminant = Math.sqrt(b*b-4*a*c);
root1 = (-b + discriminant)/(2*a);
root2 = (-b - discriminant)/(2*a);

PrintWriter out = new PrintWriter(new FileWriter("results.dat", true));

out.println("Root1: " + root1 + ", Root2: " + root2);
out.close();  // Closing a file after writing to it.

boolean b1 = Double.isNaN(root1);
boolean b2 = Double.isNaN(root2);

if(b1 || b2) {
System.out.println("Quadratic equation with the following coefficients:");
System.out.println("a: " + a + ";" + " b: " + b + ";" + " c: " + c + ";");
System.out.println("has roots that don't exist.");
}
}```