简体   繁体   English

While循环读取csv文件越界

[英]While-loop reading csv file out of bounds

I am working on a program that reads from a file with a list of bank accounts.我正在开发一个从包含银行帐户列表的文件中读取的程序。 I keep encountering an IndexOutOfBoundsException error and I'm not sure why.我一直遇到 IndexOutOfBoundsException 错误,我不知道为什么。 When I put the bank accounts straight in the code (not reading from a file) it works just fine which leads me to believe that my while-loop is messed up somewhere.当我将银行账户直接放入代码中(而不是从文件中读取)时,它工作得很好,这让我相信我的 while 循环在某处搞砸了。 I suspect that it is not looping correctly but I am not positive.我怀疑它没有正确循环,但我并不乐观。 I don't have very much experience with while-loops unfortunately.不幸的是,我对 while 循环没有太多经验。

File I am reading from, listed in order of accountType/name/worth/rate:我正在阅读的文件,按 accountType/name/worth/rate 的顺序列出:

1,Waterford Ellingsworth,4350.0,0.002
2,Bethanie Treadwell,500.0,0.35
3,Ira Standish,50000,0.1,59,0.1

The actual program itself:实际程序本身:

 Scanner infile = new Scanner("‪C:/Users/Josh/Desktop/prog5input.csv");
    while (infile.hasNextLine()) {
        Scanner s2 = new Scanner(infile.nextLine()); // put string from file into second Scanner object
        s2.useDelimiter(",");
        if (s2.hasNextInt()) {
            int type = s2.nextInt(); 
            if (type == 1) {
                String accountHolder = s2.next();
                double accountInitial = s2.nextDouble();
                double accountRate = s2.nextDouble();
                bank.addNewAccount(new SavingsAccount(accountHolder, accountInitial, accountRate));
                // read fields for Type 1 accounts and create new Type 1 object
            }
            else if (type == 2) {
                String accountHolder = s2.next();
                double accountInitial = s2.nextDouble();
                double accountCostPerMonth = s2.nextDouble();
                bank.addNewAccount(new CheckingAccount(accountHolder, accountInitial, accountCostPerMonth));
            }
            else if (type == 3) {
                String accountHolder = s2.next();
                double accountInitial = s2.nextDouble();
                double accountRate = s2.nextDouble();
                int disbursementAge = s2.nextInt();
                double earlyWithdrawalPenalty = s2.nextDouble();
                bank.addNewAccount(new IRAAccount(accountHolder, accountInitial, accountRate, disbursementAge, earlyWithdrawalPenalty));
            }
        } //ends if statement s2.hasNextInt()
        // now go to top of loop, check infile to see if there is another line to read
    } //ends while loop

Edited to include the output:编辑以包括输出:

Creating accounts...

Performing transactions...
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Bank.getAccountByIndex(Bank.java:28)
    at BankDriverFileInput.main(BankDriverFileInput.java:58)

Here's a link to my entire program: https://drive.google.com/drive/folders/0BysdkYqrEP7kazNhMXpQSzd4bUE?usp=sharing这是我整个程序的链接: https : //drive.google.com/drive/folders/0BysdkYqrEP7kazNhMXpQSzd4bUE?usp=sharing

replace代替

Scanner infile = new Scanner("‪C:/Users/Josh/Desktop/prog5input.csv");

with

Scanner infile = new Scanner(new FileInputStream("‪C:/Users/Josh/Desktop/prog5input.csv"));

beacuse as your code, the inputstream of Scanner will be String "c:...." rather than the file it represents.怎么一回事,因为是你的代码,扫描仪的输入流字符串“C:......”,而不是它所代表的文件

I downloaded your google drive programs and it worked for me with charm with following change..我下载了你的谷歌驱动程序,它对我有用,有以下变化。

Scanner infile = new Scanner(new FileInputStream("‪C://Users//Josh//Desktop//prog5input.csv"));

Make sure you have specified correct file path here..确保您在此处指定了正确的文件路径..

My sample output for your reference:我的示例输出供您参考:

Program 5, Josh Correia, cssc0491 Creating accounts... Waterford Ellingsworth4350.00.002Customer: Waterford Ellingsworth, Savings account 1 Balance: $4350.00 Customer: Bethanie Treadwell, Checking account 2 Balance: $500.00 Customer: Ira Standish, IRA Savings account 3 Balance: $50000.00, Disbursement Age=59, Early Withdrawal Penalty=0.1程序 5,Josh Correia,cssc0491 创建账户... Waterford Ellingsworth4350.00.002客户:Waterford Ellingsworth,储蓄账户 1 余额:4350.00 美元客户:Bethanie Treadwell,支票账户 2 余额:500.00 美元客户:Ira Standish,IRA 储蓄账户 30.50 美元, 支付年龄=59,提前退出罚款=0.1

Performing transactions... Savings account 1 Balance: $4550.00 Checking account 2 Balance: $286.87 IRA Savings account 3 Balance: $50000.00, Disbursement Age=59, Early Withdrawal Penalty=0.1执行交易... 储蓄账户 1 余额:4550.00 美元支票账户 2 余额:286.87 美元 IRA 储蓄账户 3 余额:50000.00 美元,支付年龄 = 59,提前取款罚款 = 0.1

Updating accounts... Savings account 1 Balance: $4559.10 Checking account 2 Balance: $286.52 IRA Savings account 3 Balance: $55000.00, Disbursement Age=59, Early Withdrawal Penalty=0.1正在更新账户... 储蓄账户 1 余额:4559.10 美元支票账户 2 余额:286.52 美元 IRA 储蓄账户 3 余额:55000.00 美元,支付年龄=59,提前取款罚款=0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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