从ATM程序的文件中读取文本

时间:2014-12-17 18:26:56

标签: java

我正在通过Cay Horstmann的书Big Java早期对象工作,我遇到了工作示例编号12的问题。我想从文本文件中读取信息,但代码似乎对我不起作用。我做错了什么?

以下是代码:

package javaprogrammingproject;

import java.io.IOException;
import java.util.Scanner;

/**
 * A text-based simulation of an automatic teller machine.
 */
public class ATMSimulator {

    public static void main(String[] args) {
        ATM theATM;

        try {
            Bank theBank = new Bank();
            theBank.readCustomers("customers.txt");
            theATM = new ATM(theBank);
        } catch (IOException e) {
            System.out.println("Error opening accounts file.");
            return;
        }

        Scanner in = new Scanner(System.in);

        while (true) {
            int state = theATM.getState();

            if (state == ATM.START) {
                System.out.print("Enter customer number: ");
                int number = in.nextInt();
                theATM.setCustomerNumber(number);
            } else if (state == ATM.PIN) {
                System.out.print("Enter PIN: ");
                int pin = in.nextInt();
                theATM.selectCustomer(pin);
            } else if (state == ATM.ACCOUNT) {
                System.out.print("A=Checking, B=Savings, C=Quit: ");
                String command = in.next();

                if (command.equalsIgnoreCase("A")) {
                    theATM.selectAccount(ATM.CHECKING);
                } else if (command.equalsIgnoreCase("B")) {
                    theATM.selectAccount(ATM.SAVINGS);
                } else if (command.equalsIgnoreCase("C")) {
                    theATM.reset();
                } else {
                    System.out.println("Illegal input!");
                }
            } else if (state == ATM.TRANSACT) {
                System.out.println("Balance=" + theATM.getBalance());
                System.out.print("A=Deposit, B=Withdrawal, C=Cancel: ");
                String command = in.next();

                if (command.equalsIgnoreCase("A")) {
                    System.out.print("Amount: ");
                    double amount = in.nextDouble();
                    theATM.deposit(amount);
                    theATM.Back();
                } else if (command.equalsIgnoreCase("B")) {
                    System.out.print("Amount: ");
                    double amount = in.nextDouble();
                    theATM.withdraw(amount);
                    theATM.Back();
                } else if (command.equalsIgnoreCase("C")) {
                    theATM.Back();
                } else {
                    System.out.println("Illegal input!");
                }
            }
        }
    }
}

此处还有该方法的代码:

public void readCustomers(String filename) throws IOException {
    Scanner in = new Scanner(new File(filename));

    while (in.hasNext()) {
        int number = in.nextInt();
        int pin = in.nextInt();
        Customer c = new Customer(number, pin);
        addCustomer(c);
    }

    in.close();
}

这就是文本文件中的内容:

1 1234 0 1
2 2468 0 1
3 3692 2 3
4 4826 4 3
5 5050 5 6
6 6284 5 7
7 7418 5 8
8 8642 9 10
9 9876 11 12

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

在文件中,第3列和第4列是什么?当您在while循环的第二次迭代中执行in.nextInt()时,您将获得0/1而不是2/2468,您希望创建客户对象。要么逐行读取文件要么跳过2个整数来创建下一个客户