0

Having trouble trying to get this code working! Please help!

package com.mtndewey.calculator;

import java.util.Scanner;

public class Calculator {

private static double inputA, inputB, inputC;

public static void main(String[] args) {

    final Scanner ScannerA = new Scanner(System.in);
    final Scanner ScannerB = new Scanner(System.in);
    final Scanner ScannerC = new Scanner(System.in);

    System.out.println("Enter the A value");
    final double inputA = ScannerA.nextDouble();
    System.out.println("Enter the B value");
    final double inputB = ScannerA.nextDouble();
    System.out.println("Enter the C value");
    final double inputC = ScannerA.nextDouble();

    getComponents(inputA, inputB, inputC);
    double[] answers = getAnswers();

    if (answers != null) {
        if (inputA == 0 && inputB != 0) {// if they input a linear equation
            System.out.print("This is a linear equation, x equals "
                    + (-inputC / inputB) + ".");
            System.exit(0);
        }
        if (inputA == 0 && inputB == 0 && inputC != 0) {// if they put just
                                                        // the constant
            System.out.print("No solution.");
            System.exit(0);

        }
        if (inputA == 0 && inputB == 0 && inputC == 0) {// if they put only
                                                        // zeros
            System.out.print("Zero equals zero.");
            System.exit(0);
        }
        if (answers[0] == answers[1] && inputA != 0) {// outputs only one
                                                        // for no repetition
            System.out.println("x equals: ");
            System.out.print(answers[0]);
            System.exit(0);
        }
        if (answers[0] != answers[1] && inputA != 0) {// normal input
            System.out.println("x equals: " + answers[0] + ", " + answers [1]);

        }

        else {
            System.out.println("Error");
        }
    }
    if (answers == null) {
        System.out.print("Imaginary answer!");
    }
}

public static double[] getComponents(double a, double b, double c) {

    double discriminant = (b * b) - (4 * a * c);
    double component1 = b * -1; // negative b
    double component2 = Math.sqrt(discriminant);
    double component3 = component2 / (2 * a);
    double[] components = { component1, discriminant, component3 };

    return components;

}

public static boolean isImaginary() {
    double[] components = getComponents(inputA, inputB, inputC);
    double discriminant = components[1];

    if (discriminant < 0) {
        return true;
    } else
        return false;
}

public static double[] getAnswers() {
    double[] components = getComponents(inputA, inputB, inputC);
    double component1, component3;
    component1 = components[0];
    component3 = components[2];

    if (isImaginary()) {
        return null;
    } else {// Answers

        double answerplus = component1 + component3;
        double answerminus = component1 - component3;
        double[] answers = { answerplus, answerminus };

        return answers;
    }
}

}

SSC
  • 1,311
  • 5
  • 18
  • 29
Jon Dewey
  • 25
  • 6
  • 1
    You should be able to avoid the "post is mostly code" message if you give additional details in your post, such as what `a`, `b`, and `c` are when `NaN` occurs. I suspect that the `NaN` results from such inputs that describe a quadratic equation with no real roots. `Math.sqrt` returns `NaN` when the argument is negative. – rgettman Jan 14 '15 at 01:05
  • 1
    Welcome to StackOverflow. This site can be incredibly useful, but it is also important to make sure that you use it correctly. You should take a look at the site tour to make sure that you understand how things work here, but just to make sure you don't get downvoted-- edit your post and try to provide as many details as possible and be as specific as you can be in order to get the best and most accurate support – badfilms Jan 14 '15 at 01:06
  • Please edit the question and explain why it doesn't work. – tbodt Jan 14 '15 at 01:08
  • when do you get the nan error? for some inputs? every input? for inputs that should have imaginary roots? for inputs that shouldnt have imaginary roots? what is com.mtndewey.calculator? this post is mostly code" warning is very apt here. – chiliNUT Jan 14 '15 at 01:12

1 Answers1

0

For some reason you have 2 sets of variables with the same names.

You have static variables inputA, inputB, inputC that are never assigned (so have the default value 0.0).

You also have instance variables inputA, inputB, inputC. These are assigned, using the values you enter.

Unfortunately whatever values you enter, getAnswers uses the static versions, so component3 is calculated from 0.0/0.0 which is NaN (not a number).

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116