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.

Results 1 to 9 of 9

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

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question 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!
    Last edited by UniverseCloud; January 12th, 2019 at 12:53 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,328
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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");
           }
       }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,328
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

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

    UniverseCloud (January 12th, 2019)

  6. #5
    Junior Member
    Join Date
    Aug 2017
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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");
           }
       }
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,328
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default 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.

  9. #8
    Junior Member
    Join Date
    Aug 2017
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with this code ( an exercise )

    Quote Originally Posted by Norm View Post
    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!

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,328
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 09:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 07:42 PM
  3. What is wrong with my code
    By chaffee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 18th, 2013, 06:39 AM
  4. Employee Class Exercise... Code will not run!!!!
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 26th, 2012, 02:39 PM