-1
import java.util.Scanner;

public class Main {

    static double a, b, c, x, y, AOS;

    public static void main(String[] args) {
        Main.getA();
    }

    public static void getA() {
        Scanner inputA = new Scanner(System.in);
        System.out.println("Input variable 'a'");
        a = inputA.nextDouble();
        inputA.close();

        System.out.println("A: " + a);

        Main.getB();
    }
    public static void getB() {
        Scanner inputB = new Scanner(System.in);
        System.out.println("Input variable 'b'");
        b = inputB.nextDouble();
        inputB.close();

        System.out.print("B: " + b);

        Main.getC();
    }
    public static void getC() {
        Scanner inputC = new Scanner(System.in);
        System.out.println("Input variable 'c'");
        c = inputC.nextDouble();
        inputC.close();

        System.out.print("C: " + c);

        Main.getAOS();
    }
    public static void getAOS() {
        AOS = (-b + Math.sqrt((b*b)-4*a*c)) / 2*a;
        System.out.println("AOS: " + AOS);

        Main.getPoint1();
    }
    public static void getPoint1() {
        x = AOS;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("Origin: (" + x + "," + y + ")");

        Main.getPoint2();
    }
    public static void getPoint2() {
        x = AOS + 1;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("1: (" + x + "," + y + ")");

        Main.getPoint3();
    }
    public static void getPoint3() {
        x = AOS - 1;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("2: (" + x + "," + y + ")");

        Main.getPoint4();
    }
    public static void getPoint4() {
        x = AOS + 2;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("3: (" + x + "," + y + ")");

        Main.getPoint5();
    }
    public static void getPoint5() {
        x = AOS - 2;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("4: (" + x + "," + y + ")");
    }
}

This is my code. I am trying to get user input to select the value for variables a, b, and c so the program can run the equations. Math works, but I am getting an error after the first input.

azurefrog
  • 9,994
  • 7
  • 36
  • 51

1 Answers1

1

When you close the scanner you also close the underlying stream (in your case System.in) and then you can't read from it again even after trying to access it from another instance of Scanner.

Therefore the simplest approach would be to use only one Scanner object instead of many. Change your code to look like:

static double a, b, c, x, y, AOS;
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    Main.getA();
}

public static void getA() {
    System.out.println("Input variable 'a'");
    a = scanner.nextDouble();
    System.out.println("A: " + a);
    Main.getB();
}

public static void getB() {
    System.out.println("Input variable 'b'");
    b = scanner.nextDouble();    
    System.out.print("B: " + b);
    Main.getC();
}
public static void getC() {
    System.out.println("Input variable 'c'");
    c = scanner.nextDouble();
    System.out.print("C: " + c);
    Main.getAOS();
}
public static void getAOS() {
    AOS = (-b + Math.sqrt((b*b)-4*a*c)) / 2*a;
    System.out.println("AOS: " + AOS);
    Main.getPoint1();
}
public static void getPoint1() {
    x = AOS;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("Origin: (" + x + "," + y + ")");
    Main.getPoint2();
}
public static void getPoint2() {
    x = AOS + 1;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("1: (" + x + "," + y + ")");
    Main.getPoint3();
}
public static void getPoint3() {
    x = AOS - 1;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("2: (" + x + "," + y + ")");
    Main.getPoint4();
}
public static void getPoint4() {
    x = AOS + 2;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("3: (" + x + "," + y + ")");
    Main.getPoint5();
}
public static void getPoint5() {
    x = AOS - 2;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("4: (" + x + "," + y + ")");
}
}

You might also want to close the Scanner without closing the System.in stream. In that case wrap System.in in a CloseShieldInputStreamas explained here

Community
  • 1
  • 1
alainlompo
  • 4,130
  • 3
  • 27
  • 39
  • @azurefrog, thanks for the note. Sure I will edit it. – alainlompo May 20 '15 at 20:13
  • 1
    I like the `CloseShieldInputStream` idea. I haven't run across that before. This would allow you to scope your input `Scanner`s tightly, instead of relying on a single static `Scanner`. – azurefrog May 20 '15 at 20:45
  • @azurefrog, yes it is a great idea. I believe it (the idea) can be reused in a pattern-like fashion – alainlompo May 20 '15 at 20:49