Intro to Java Programming

I have started taking program in Java and have run into a bit of a snag. I need to develop a program in Java that will take the quadratic formula and display based on the discriminant, display the appropreiate answer. I am submitting my code that I have come up with and would appreciate if anyone could assist me as to why I am getting the errors that I am getting (which I am also attaching.)

import java.util.Scanner;
public class Homework2A{
public static void main(String[] args){
Scanner input = new Scanner(;
System.out.println("Please enter the value of a in the equation: ");
double a = input.nextDouble();
System.out.println("Please enter the value of b in the equation: ");
double b = input.nextDouble();
System.out.println("Please enter the value of c in the equation: ");
double c = input.nextDouble();
double di;
di = b * b - 4 * a * c;
double r1;
r1 = (-b + Math.pow(di, 0.5)) / (2 * a);
double r2;
r2 = (-b - Math.pow(di, 0.5)) / (2 * a);
if (r1 > 0){
if (r2 > 0){
System.out.println("The two roots are: " + r1 + r2);
else if (r1 || r2 == 0){
System.out.println("The root is: " + r1 || r2);
else if (r1 || < 0){
System.out.println("The equation has no real roots");
} illegal start of type
else if (r1 || < 0){ <--(with the indication on the zero) illegal start of type
else if (r1 || < 0){ <---(with the indication on the outside bracket) ')' expected
else if (r1 || < 0){ <---(with the indication on the end bracket. class, interface, or enum expected


  • You should be checking to see if di<0 before doing the root computation. In all likelihood it is creating a type conflict later on. I haven't programmed in java in a while, but there may also be a problem with the
    || operator and the < operator being next to each other. I forget, does || indicate logical "and" or "or" or something else?
    Also there is no 'e' in the middle of "appropriate". Good luck. There may be other errors I didn't find. There usually are. But definitely do the check on di before you try to find its .5 power or square root, because that will cause a crash.

    TomTeacher (retired)
  • Whoops!
    I would also use the form

    if (r1<0) || (r2<0) then.......

    the form if (r1 || r2<0) then.....

    will probably cause the computer to check the truth value of r1, then the truth value of r2<0, thus generating a type conflict because r1 is not a boolean type.

    But this point is moot because you won't be using the first expression above. But But it is probably generating an error in its present form.

    Again, good luck.


  • Hey,
    [code]} else if (r1 || r2 == 0) {[/code]
    should be coded
    [code]} else if (r1 == 0 || r2 == 0) {[/code]
    if the goal is to see if r1 or r2 is 0.

    What do you want the answer to be with this code?
    [code]System.out.println("The root is: " + r1 || r2);[/code]
    based on the if test I would guess:
    [code]System.out.println("The root is: " + (r1 + r2));[/code]

    The following statement does not "resolve" to a boolean, that is true or false.
    [code]} else if (r1 || < 0) {[/code]
    perhaps what is desired is:
    [code] else if (r1 < 0) {[/code]

    regards, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!