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

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: What is wrong with this code ( an exercise )

1. ## What is wrong with this code ( an exercise )

So the exercise tells me to get the values a, b and c from the user and then use the quadratic formula to get both roots.

if both are above 0, display both roots
if both are 0, display 1 root
if both are negative, no real root

So all is good until this:

if (root1 && root2 > 0) {
System.out.println("The equation has two roots: " + root1 + " and " + root2);
else if(root1 && root2 == 0) {
System.out.println("The equation has one root: " + root1);
}
}else{
System.out.println("The equation has no real roots");
}

if i only placed the braces on if and else (but not else if) the compiler doesn't recognise else and wants it disposed, if i place the braces on all, it says i cant convert double into int even tho i didnt specify anything of the sort.

Could you guys help me out ? much thanks!  Reply With Quote

2. ## Re: What is wrong with this code ( an exercise )

Please edit your post and wrap your code with code tags:

[code]
**YOUR CODE GOES HERE**
[/code]

to get highlighting and preserve formatting.

Start with the first { and find its matching }
There is a } on line 1. Where is its matching }?

Do the same with the if and else.
There is an if on line 1, where is its matching else?

Please copy the full text of the error message and paste it here. It has important info about the error.  Reply With Quote

3. ## Re: What is wrong with this code ( an exercise )

Here is there errors and the full code:

2 errors found:
File: C:\Users\omar-\Desktop\java learning\ChapterThree.java [line: 17]
Error: The operator && is undefined for the argument type(s) double, boolean

File: C:\Users\omar-\Desktop\java learning\ChapterThree.java [line: 20]
Error: The operator && is undefined for the argument type(s) double, boolean

```import java.util.*;

public class ChapterThree {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter the values of a, b and c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();

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

if (root1 && root2 > 0){
System.out.println("The equation has two roots: " + root1
+ " and " + root2 );
}else if ( root1 && root2 == 0 ){
System.out.println("The equation has one root: " + root1);
}else{
System.out.println("The equation has no real roots");
}
}
}```  Reply With Quote

4. ## Re: What is wrong with this code ( an exercise )

The operator && is undefined for the argument type(s) double, boolean
The && operator works with two boolean operands: boolean1 && boolean2
In the expression:
root1 && root2 == 0
Only the expression: root2 == 0 has a boolean value
root1 is a double and not a boolean
To fix the error, change the first operand to a boolean. For example: x > 3  Reply With Quote

5. ## The Following User Says Thank You to Norm For This Useful Post:

UniverseCloud (January 12th, 2019)

6. ## Re: What is wrong with this code ( an exercise )

Sorry for the late reply! Been on and off on this question (Dont know why the its still giving me an error, a new one)
Like, i got the else if part right but when it comes to both together to be above 0 i tried this condition(other than the one on the code)
if((root1 && root2) == 0. but apparently they are both counted as double .-. so yah..

What does this error mean ?
code is the same above the if statements.

\$javac ChapterThree.java
\$java -Xmx128M -Xms16M ChapterThree
Enter the values of a, b and c:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at ChapterThree.main(ChapterThree.java:10)

```if (root1 > 0 && root2 > 0){
System.out.println("The equation has two roots: " + root1
+ " and " + root2 );
}else if ( (root1 > 0 && root2 == 0) || (root1 == 0 && root2 > 0) ){
System.out.println("The equation has one root: " + root1);
}else{
System.out.println("The equation has no real roots");
}
}
}```  Reply With Quote

7. ## Re: What is wrong with this code ( an exercise )

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at ChapterThree.main(ChapterThree.java:10)
At line 10 the program calls the nextDouble method but there isn't any double value available.

What was entered for the Scanner class's nextDouble method to read?  Reply With Quote

8. ## Re: What is wrong with this code ( an exercise )

This had nothing to do with your problem but is provided for your reading pleasure. From the Wiki on the quadratic formula.

Regards,
Jim

Avoiding loss of significance

Although the quadratic formula provides an exact solution, the result is not exact if real numbers are approximated during the computation, as usual in numerical analysis, where real numbers are approximated by floating point numbers (called "reals" in many programming languages). In this context, the quadratic formula is not completely stable.

This occurs when the roots have different order of magnitude, or, equivalently, when b2 and b2 − 4ac are close in magnitude. In this case, the subtraction of two nearly equal numbers will cause loss of significance or catastrophic cancellation in the smaller root. To avoid this, the root that is smaller in magnitude, r, can be computed as (c/a)/R where R is the root that is bigger in magnitude.  Reply With Quote

9. ## Re: What is wrong with this code ( an exercise ) Originally Posted by Norm At line 10 the program calls the nextDouble method but there isn't any double value available.

What was entered for the Scanner class's nextDouble method to read?
Well, i was using an online compiler to run this program and it seems that the compiler didnt accept scanner ?? so recently i got my laptop back and wrote the program on dr.java and it worked! It was just the online compilers fault :!

Thank you for the help norm!  Reply With Quote

10. ## Re: What is wrong with this code ( an exercise )

There is a way to use the Scanner class that does not require a console: Put the input values into the Scanner class's constructor:
` Scanner input = new Scanner("3.4 5.6 7.8"); //  Load 3 floating numbers to read with nextDouble`  Reply With Quote