.nextInt() 上的 NoSuchElementException;

NoSuchElementException on .nextInt();

提问人:catdu 提问时间:4/1/2023 最后编辑:Jonascatdu 更新时间:5/10/2023 访问量:43

问:

我正在尝试编写一个程序,该程序读取多个用户银行账户的信息并对其执行某些操作(取款、存款、显示余额)等。代码似乎是正确的,但我的方法有问题。.nextInt();

这是我的代码:

import java.io.*;
import java.util.Scanner;

public class Topic_7_HW {
    public static void printAcct(Scanner inputFile, PrintWriter outputFile, int[] acctNum, double[] balance,
            int numAccts) {
        for (int n = 0; n <= numAccts; n++) {
            outputFile.printf("\n %1f Account Number: ", acctNum[n]);
            outputFile.printf("\n %1f Account Balance: ", balance[n]);
        }
    }

    public static int readAccts(Scanner inputFile, PrintWriter outputFile, int[] acctNum, double[] balance) {
        int numAccts = 0;
        while (inputFile.hasNext()) {
            acctNum[numAccts] = inputFile.nextInt();
            balance[numAccts] = inputFile.nextDouble();
            numAccts++;
        }
        return numAccts;
    }

    public static int findAcct(Scanner inputFile, PrintWriter outputFile, int[] acctNum, int numAccts, int account) {
        account = inputFile.nextInt();
        if (account != -1) {
            for (int n = 0; n <= numAccts; n++) {
                if (account == acctNum[n])
                    ;
                account = acctNum[n];
            }
        } else
            outputFile.printf("\n %1s Error; requested account does not exist. ");
        return account;
    }

    public static void withdrawal(Scanner inputFile, PrintWriter outputFile, int[] acctNum, double[] balance,
            int numAccts) {
        int account = -1;
        double wd = -1.0;
        account = findAcct(inputFile, outputFile, acctNum, numAccts, account);
        boolean acct_vld;
        if (account != -1)
            acct_vld = true;
        if (acct_vld = true) {
            outputFile.printf(
                    "%1d Withdrawing money from account ID: \n%1d Current balance: \n %1d Enter amount to withdraw: ",
                    acctNum[account], balance[account], wd);

            wd = inputFile.nextDouble();
            if (balance[account] < wd)
                outputFile.printf("\n %1s Invalid withdraw ammount: Amount cannot exceed balance. ");
            if (wd < 0)
                outputFile.printf("\n %1s Invalid withdraw ammount: Cannot withdraw negative dollars. ");
            else {
                balance[account] -= wd;
                outputFile.print("\n%1s Successfully withdrew $" + wd + " from account " + account);
            }
        }

    }

    public static void deposit(Scanner inputFile, PrintWriter outputFile, int[] acctNum, double[] balance,
            int numAccts) {
        int account = -1;
        double dp = 0;
        account = findAcct(inputFile, outputFile, acctNum, numAccts, account);
        boolean acct_vld;
        if (account != -1)
            acct_vld = true;
        else
            acct_vld = false;
        if (acct_vld = true) {
            outputFile.printf(
                    "%1d Depositing money into account ID: \n%1d Current balance: \n %1d Enter amount to deposit: ",
                    acctNum[account], balance[account], dp);

            dp = inputFile.nextDouble();
            if (dp < 0 || dp == 0)
                outputFile.printf("\n %1s Invalid withdraw ammount: Cannot withdraw negative/zero dollars. ");
            else {
                balance[account] += dp;
            }
            outputFile.print("\n%1s Successfully deposited $" + dp + " Into account " + account + ". new balance is: "
                    + balance[account] + ".");
        }

    }

    public static int newAcct(Scanner testCases, PrintWriter outputFile, int[] acctNum, double[] balance,
            int numAccts) {
        int account = -1;
        boolean exist;
        outputFile.printf("Creating New account. \n Please enter account number.");
        for (int n = 0; n <= numAccts; n++) {
            if (account == acctNum[n])
                ;
            exist = true;
        }
        if (exist = true) {
            outputFile.printf("\nChoose new ID. Account already exists.");
            return account;
        } else {
            acctNum[account] = testCases.nextInt();
            balance[account] = testCases.nextDouble();
            outputFile.printf("\n Account successfully created. Account number is: %1d Current Balance is %1.2d",
                    acctNum[account], balance[account]);
            return account;
        }

    }

    public static void balance(Scanner inputFile, PrintWriter outputFile, int[] acctNum, double[] balance,
            int numAccts) {
        int account = -1;
        account = findAcct(inputFile, outputFile, acctNum, numAccts, account);
        for (int n = 0; n <= numAccts; n++) {
            if (account == acctNum[n]) {
                account = acctNum[n];
                outputFile.printf("%1d", balance[n]);
            } else
                outputFile.printf("%1s Error; requested account does not exist");

        }
    }

    public static void menu() {
        System.out.println("Please make a selection.");
        System.out.println("W - Withdraw");
        System.out.println("D - Deposit");
        System.out.println("B - Check Balance");
        System.out.println("N - Make New Account");
        System.out.println("Q - Quit");
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        PrintWriter outputFile;
        outputFile = new PrintWriter("hw_7_out.txt");
        File myinputFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic_7\\src\\hw7input.txt");
        Scanner inputFile = new Scanner(myinputFile);
        File mytestcases = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic_7\\src\\hw7testcases.txt");
        Scanner testCases = new Scanner(mytestcases);
        char slct;
        final int max_acct = 50;
        int notDone = 20;
        double[] balance;
        balance = new double[max_acct];
        int[] acctNum;
        acctNum = new int[max_acct];
        int numAccts;
        numAccts = readAccts(inputFile, outputFile, acctNum, balance);
        // create a loop that calls the menu method and calls the selected action method
        do {
            menu();
            String choice = testCases.next();
            slct = choice.charAt(0);
            switch (slct) {
            // tell switch statement which method to call depending on the user's input
            case 'W', 'w': withdrawal(inputFile, outputFile, acctNum, balance, numAccts);
                break;
            case 'D', 'd': deposit(inputFile, outputFile, acctNum, balance, numAccts);
                break;
            case 'B', 'b': balance(inputFile, outputFile, acctNum, balance, numAccts);
                break;
            case 'N', 'n': newAcct(inputFile, outputFile, acctNum, balance, numAccts);
                break;
            // declare notDone as the numerical value for F if user wants to quit
            case 'Q', 'q':
                notDone = 6;
                break;
            // prints an error if the input is anything other than the availible selections
            default:
                outputFile.printf("\n%1s", "Error; Invalid selection.");
            }
        } while (notDone == 20);
        printAcct(inputFile, outputFile, acctNum, balance, numAccts);
        outputFile.printf("In total, there are " + numAccts + " accounts.");
        outputFile.flush();
        inputFile.close();
        outputFile.close();
        testCases.close();
    }

}

以下是从 inputFile 中读取的内容:

2616 19788.01
7187 3402.67
9685 210.99
1190 762062.84
9013 23186.65
2257 307.77
9062 16.4
8836 340837.14
9439 26802.80
2435 2025.25
7563 80687.21

它应该读取帐号并将其作为下一个 int,但相反,我在行上得到了一个 nosuchelementexception;24、40 和 159 分别是(按顺序):

 24 account = inputFile.nextInt();
 40 account = findAcct(inputFile, outputFile, acctNum, numAccts, account);
159 case 'W', 'w': withdrawal(inputFile, outputFile, acctNum, balance, numAccts);
java.util.scanner nosuchelementexception

评论

0赞 Erwin 4/1/2023
我认为您不应该在从 inputFile 中读取的数字上使用,请尝试使用nextInt()nextBigDecimal()nextDouble()
0赞 Erwin 4/1/2023
输入文件上的数字似乎不一致
0赞 catdu 4/1/2023
我试图将第一个数字读取为帐户 ID,将第二个数字读取为余额。当我提交问题时,格式搞砸了,但它应该是每行 2 个数字。例如,一行是 2265(帐号)1167.00(余额),数字都是随机的
0赞 Erwin 4/1/2023
我已经尝试了您的代码并且它工作正常,我认为问题出在您的文本文件中。再次检查路径和文件。
0赞 Anonymous 4/1/2023
我没有阅读你所有的代码(对于堆栈溢出问题来说,这真的太多了),但我怀疑:你一直在阅读输入文件直到最后。因此,当您尝试从同一输入文件中读取帐号时,没有什么可读取的了。难道是这样吗?readAccts()findAcct()

答: 暂无答案