0

I am learning Java after my first year as a computer science major using Python exclusively. I am making a basic program that solves quadratic equations. I am having issues with a variable not initializing when an if-statement is present in the program. Here is the code that doesn't compile:

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

        String input;
        do
        {

            Scanner in = new Scanner(System.in);
            System.out.println("A: ");
            double a = in.nextDouble();
            System.out.println("B: ");
            double b = in.nextDouble();
            System.out.println("C: ");
            double c = in.nextDouble();

            if ((a == 0) && (b == 0) && (c == 0))
            {
                System.out.println("No solutions to quadratic formual with these inputs!");
                continue;
            }

            double d = b * b - 4*a*c;
            double x1 = (-b - Math.sqrt(d)) / (2 * a);
            double x2 = (-b + Math.sqrt(d)) / (2 * a);
            System.out.println(x1);
            System.out.println(x2);

            System.out.println("Continue? ");

            input = in.next();

        }
        while (input.equals("Y"));

    }
}

If I remove the if-statement, it compiles correctly. If I give the String input a default value of "", it also works correct (with the if-statement included). Why is this? This seems basic but I can't figure it out for the life of me!

2 Answers2

1

input = in.next(); is never executed if the if statement evaluates to true (since the body of the if statement contains a continue statement, which skips to the end of the while loop's iteration), so the while loop's condition cannot test this variable (which may not be initialized).

You can avoid this error by initializing input to an empty String:

String input = "";

As an alternative, it might make more sense to change your logic as follows, since you probably want to display the "Continue?" question even if the numeric inputs are all 0.

public static void main(String[] args)
{

    String input;
    do
    {

        Scanner in = new Scanner(System.in);
        System.out.println("A: ");
        double a = in.nextDouble();
        System.out.println("B: ");
        double b = in.nextDouble();
        System.out.println("C: ");
        double c = in.nextDouble();

        if ((a == 0) && (b == 0) && (c == 0)) {
            System.out.println("No solutions to quadratic formual with these inputs!");
        } else {
            double d = b * b - 4*a*c;
            double x1 = (-b - Math.sqrt(d)) / (2 * a);
            double x2 = (-b + Math.sqrt(d)) / (2 * a);
            System.out.println(x1);
            System.out.println(x2);
        }
        System.out.println("Continue? ");
        input = in.next();
    }
    while (input.equals("Y"));
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

You've declared a variable input that is not initialized until the last statement of the do-while block. If you introduce the possibility of a continue before this statement, then input could be uninitialized when referenced in the condition.

It is not the if statement that is causing this error; it is the continue statement.

I would remove the continue and place the solution calculations in an else block to avoid dividing by 0. Then I would make sure that the last 2 statements asking the user to continue are past the if/else to ensure that input is initialized by the time the while condition is evaluated.

    if ((a == 0) && (b == 0) && (c == 0))
    {
        System.out.println("No solutions to quadratic formula with these inputs!");
    }
    else
    {
        double d = b * b - 4*a*c;
        double x1 = (-b - Math.sqrt(d)) / (2 * a);
        double x2 = (-b + Math.sqrt(d)) / (2 * a);
        System.out.println(x1);
        System.out.println(x2);
    }

    System.out.println("Continue? ");

    input = in.next();
}
while (input.equals("Y"));

You may also want to detect if the discriminant is less than zero, to indicate that there are no real solutions or to give complex solutions.

You may also want to detect if just a is 0 and solve the linear equation.

rgettman
  • 176,041
  • 30
  • 275
  • 357