简体   繁体   中英

Error in simple java program

I am working on teaching myself java and while working on a code using classes I ran into this error

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at StateCalculator.getOperand(StateCalculator.java:29)
    at StateCalculator.main(StateCalculator.java:77)

Below is my code:

import java.util.Scanner;


public class StateCalculator {
    private double currentValue = 0;

    //Initialize to 0
    public StateCalculator() {
    }

    public static int displayMenu() {
        Scanner keyboard = new Scanner(System.in);
        int menuChoice = 0;

        do {
            System.out.print("Menu\n 1. Add\n 2. Subtract\n 3. Multiply\n 4. Divide\n 5.Clear\n 6. Quit\n What would you like to do?: ");
            menuChoice = keyboard.nextInt();
        } while(menuChoice < 1 || menuChoice > 6);

        keyboard.close();
        return menuChoice;
    }

    public static double getOperand(String prompt) {
        Scanner input = new Scanner(System.in);
        double operand = 0;

        System.out.print(prompt);
        operand = input.nextDouble();

        input.close();
        return operand;
    }

    public double getCurrentValue() {
        return currentValue;
    }

    public void add(double operand) {
        currentValue += operand;
    }

    public void subtract(double operand) {
        currentValue -= operand;
    }

    public void multiply(double operand) {
        currentValue *= operand;
    }

    public void divide(double operand) {
        if(operand == 0) {
            currentValue = Double.NaN;
        }
        else {
            currentValue /= operand;
        }
    }

    public void clear() {
        currentValue = 0;
    }


    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        StateCalculator calculator = new StateCalculator();
        int option;
        double operand;

        do{
            System.out.println("The current value is " + calculator.currentValue);
            option = StateCalculator.displayMenu();

            switch(option) {
            case 1:
                operand = getOperand("What is the second number?: ");
                calculator.add(operand);
                break;
            case 2:
                operand = getOperand("What is the second number?: ");
                calculator.subtract(operand);
                break;
            case 3:
                operand = getOperand("What is the second number?: ");
                calculator.multiply(operand);
                break;
            case 4:
                operand = getOperand("What is the second number?: ");
                calculator.divide(operand);
                break;
            case 5:
                calculator.clear();
                break;
            }

        }while(option != 6);

        keyboard.close();
    }

}

I tried running the debug feature in eclipse and discovered the problem occurs on line 29 in my getOperand method when I attempt to set operand = input.nextDouble. However, I don't understand why this would be an issue.

Don't call keyboard.close(); when you close keyboard (which you defined)

Scanner keyboard = new Scanner(System.in);

it closes System.in , and then your other methods can't work (because the console will not re-open). You can have multiple scanners on System.in (as long as you don't close them), or pass one (or, but please don't, use a global).

Per the javadoc ,

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM