在我的程序中获取IllegalStateException?

时间:2018-05-09 06:20:46

标签: java while-loop exception-handling illegalstateexception

Stacktrace Here

import java.util。*;

公共类AccountClient {

public static void main(String[] args)  {
    Scanner input = new Scanner(System.in);
    boolean infiniteLoop = true;
    boolean invalidInput;
    int id;
    // Create array of different accounts
    Account[] accountArray = new Account[10];
    //Initialize each account array with its own unique id and a starting account balance of $100
    for (int i = 0; i < accountArray.length; i++) {
        accountArray[i] = new Account(i, 100);
    }
    do {    
        try {       
            //inner loop to detect invalid Input
            do {
                invalidInput = false;
                System.out.print("Enter an id: ");
                id = input.nextInt(); 
                if (id < 0 || id > 9) {
                    System.out.println("Try again. Id not registered in system. Please enter an id between 0 and 9 (inclusive).");
                    invalidInput = true;
                    input.nextLine();
                }           
            } while (invalidInput);
            boolean exit;
            do {
                exit = false;
                boolean notAnOption;
                int choice;
                do {
                    notAnOption = false;
                    System.out.print("\nMain Menu\n1: check balance\n2: withdraw\n3: deposit\n4: exit\nEnter a choice: ");
                    choice = input.nextInt();
                    if (choice < 1 || choice > 4) {
                        System.out.println("Sorry, " + choice + " is not an option. Please try again and enter a number between 1 and 4 (inclusive).");
                        notAnOption = true;
                    }
                } while(notAnOption);
                switch (choice) {
                case 1: System.out.println("The balance for your account is $" + accountArray[id].getBalance());
                    break;
                case 2: {
                    boolean withdrawFlag;
                    do {
                        System.out.print("Enter the amount you would like to withdraw: ");
                        double withdrawAmount = input.nextInt();
                        if (withdrawAmount > accountArray[id].getBalance()) {
                            System.out.println("Sorry, you only have an account balance of $" + accountArray[id].getBalance() + ". Please try again and enter a number at or below this amount.");
                            withdrawFlag = true;
                        }
                        else {
                            accountArray[id].withdraw(withdrawAmount);
                            System.out.println("Thank you. Your withdraw has been completed.");
                            withdrawFlag = false;
                        }
                    } while (withdrawFlag);
                }

                    break;
                case 3: {
                    System.out.print("Enter the amount you would like to deposit: ");
                    double depositAmount = input.nextInt();
                    accountArray[id].deposit(depositAmount);
                    System.out.println("Thank you. You have successfully deposited $" + depositAmount + " into your account.");
                    }
                    break;
                case 4: {
                    System.out.println("returning to the login screen...\n");
                    exit = true;
                    }
                    break;
                }

            } while (exit == false);

        }

        catch (InputMismatchException ex) {
            System.out.println("Sorry, invalid input. Please enter a number, no letters or symbols.");
        }
        finally {
            input.close();          
            }

        } while (infiniteLoop);
    } 


}

异常代码:

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Scanner.java:1070)
    at java.util.Scanner.next(Scanner.java:1465)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at playground.test.main.Main.main(Main.java:47)

您好,我制作了一个基本程序,它使用一个名为account的类来模拟ATM机。如果用户没有输入信件,我想抛出异常。这工作正常,但是我需要让它循环,所以程序在抛出异常后没有终止。要做到这一点,我只是把try catch放在我之前的do while循环中。但是,当我这样做的时候,每当我输入一个字母或者选择退出内部循环时它会抛出一个IllegalStateException,这会让用户回到要求他们输入id的循环。什么是IllegalStateException,在我的情况下导致它的原因是什么,我将如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:3)

相当简单,在捕获异常后,finally子句被执行。不幸的是,您在此子句中关闭了扫描程序,而Scanner.close()关闭了基础输入流(在这种情况下为System.in)。

标准输入流System.in一旦关闭,就无法再次打开。

要解决此问题,您必须省略finally子句并在程序需要终止时关闭扫描程序,而不是更早。