0

The code below is supposed to be going through a text file of 10000 bank records in the following format: "0000,XXXXXXXX,00000.00\n"

and printing the results. What it is currently doing is correctly looping until the first non default entry in the text file, printing it successfully, and then throwing "java.lang.ArrayIndexOutOfBoundsException: 1" exception. I am at a loss as to why or how this exception is being thrown.

I've tried changing the while loop to a for(s=reader.readLine();s!=null;s=reader.readLine()); loop and got same exception but without any successful output first.

Have searched and searched this exception and it always leads to basic off by one errors, but I can't for the life of me figure out why it would be doing this here.

Any help is appreciated, Thanks

import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
public class ReadBankAccountsSequentially {
    public static void main(String[] args) {
        Path file = Paths.get("/root/sandbox/BankAccounts.txt");

        // Write your code here
        String delimiter = ",";
        try {
            InputStream input = new BufferedInputStream(Files.newInputStream(file));
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            String s=reader.readLine();
            while(s!=null) {
                String[] array = s.split(delimiter);
                if(!(array[0].equals("0000"))) {
                    System.out.println("ID#" + array[0] + "  " + array[1] + "   $" + Double.parseDouble(array[2]));
                }
                s = reader.readLine();
            }
            reader.close();
        } catch(Exception e) { 
            System.out.println(e);
        }
    }
}

0 Answers0