Java BufferedReader 切换大小写同一行存储在数组中

标签 java arraylist switch-statement bufferedreader

我正在尝试循环遍历一个文件,将文件中的“帐户”读取到名为 accounts 的数组列表中。这是文件:

c 1111 1234 703.92
x 2222 1234 100.00
s 3333 1234 200.08

我知道它循环到下一行,因为我打印了 id,它从 1111 到 2222 再到 3333,但在底部,当我打印帐户的 tostring 时,它们都是相同的(“s”行在文件中)。为什么会发生这种情况?

银行.main:

private static ArrayList<Account> accounts = new ArrayList<Account>();
public static void main(String[] args)
{
    //if incorrect # or bankfile can't be opened, print to system error
    //Usage: java Bank bankFile [batchFile]

    if(args.length > 2 || args.length == 0){
        System.err.println("Usage: java Bank bankFile [batchFile]");
    }
    else{
        if(args.length == 2){ //batch mode
            processAccounts(args[0]);
            //BankBatch.processBatch(args);
            close(args[0]);
        }

        else if(args.length == 1){ // gui mode

        }
    }

}

银行.processAccounts:

private static void processAccounts(String filepath){
    BufferedReader accountReader;
    File accountFile = new File(filepath);
    if(accountFile.isDirectory()){
        System.err.println("Usage: java Bank bankFile [batchFile]");
        System.exit(1);
    }

    if(accountFile.exists()){
        try{
            accountReader = new BufferedReader(new FileReader(accountFile));

            String line = accountReader.readLine();
            while(line != null && line != ""){//in case there are extra empty lines
                String[] s = line.split(" "); // parts of an account line: type, id, pin, balance
                if(s.length != 4) break; //incorrect format, shouldn't happen
                int id = Integer.parseInt(s[1]);
                System.out.println(line);
                int pin = Integer.parseInt(s[2]);
                double balance = Double.parseDouble(s[3]);

                switch(s[0]){ //account type
                    case "x": 
                        System.out.println("case x");
                        accounts.add(new checkingAccount(id, pin, balance));
                        break;

                    case "c":
                        System.out.println("case c");
                        accounts.add(new cdAccount(id, pin, balance));
                        break;

                    case "s":
                        System.out.println("case s");
                        accounts.add(new savingsAccount(id, pin, balance));
                        break;
                }
                line = accountReader.readLine();
            }
            System.out.println(accounts.get(0).toString());
            System.out.println(accounts.get(1).toString());
            System.out.println(accounts.get(2).toString());
            accountReader.close();
        }
        catch(FileNotFoundException e){
            //this shouldn't happen since we check to see if it exists
        }
        catch (IOException e) {
        }

    }
}

帐户.java:

public abstract class Account {
public Account(int id, int pin, double bal){
    Account.balance = bal;
    Account.pin = pin;
    Account.id = id;
}

public String toString(){
    return (acctType + " " + id + " " + pin + " " + balance);
}

扩展帐户的示例,所有帐户都是相似的:

public class checkingAccount extends Account {

public checkingAccount(int id, int pin, double bal){
    super(id, pin, bal);
    setAcctType('x');
    setMin(50);
}


/**
 * apply montly interest
 * checkings accounts have no interest, only penalties for having less than minimum
 */
@Override
public double applyInterest() { //only penalty with a checking account
    if (getBalance() > 5){
        withdraw(5);
        return -5;
    }
    else{
        double change =  Math.round((getBalance() * 0.10) * 100.0) / 100.0; //10% of balance, round to 2 decimal places
        withdraw(change);
        return -change;
    }
}
}

这是我得到的输出:

c 1111 1234 703.92
case c
x 2222 1234 100.00
case x
s 3333 1234 200.08
case s
s 3333 1234 200.08
s 3333 1234 200.08
s 3333 1234 200.08
========= Final Bank Data ==========

Acount Type   Account   Balance   
-----------   -------   -------
 Saving      3333    200.08
 Saving      3333    200.08
 Saving      3333    200.08

====================================

最佳答案

查看您的 Account.java,看起来您可能已将其中的变量设为静态(但我不确定,因为您的代码片段中缺少它们的声明)。

意思是,每次构建帐户时,这些静态变量都会设置为新变量。因此,每次访问每个对象中的这些变量时,它们都是相同的。

如果我所做的所有这些假设都是正确的,那么您所要做的就是从您的 Account 变量声明中删除 static 关键字,并从 Account.something< 更改您的访问器this.something

关于Java BufferedReader 切换大小写同一行存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33465553/

相关文章:

java - Swing平移刻度更改顺序错位

java - 以编程方式注册 spring bean 缺少依赖项

java - for 循环缺少 return 语句

java - ArrayList 使用比较器对对象进行排序

C - 为什么只有第二个命令有效?

c++ - 在 switch 条件中从类到枚举类型的隐式转换

java - 关闭并打开应用程序Webview后恢复上一页

java - 找到 GPA 最高的学生

mysql - 从表中删除重复行后,从查询结果中返回唯一列表

c# - 更好地处理连接到互联网的方法中发生的异常