-1

I am trying to create an ArrayList that reads a .csv file, processes the data into an ArrayList, and then print the list out.

My code so far.

The BankRecords class

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

public class BankRecords
{
String sex, region, married, save_act, current_act, mortgage, pep;
int children;
double income;
private String id;
private int age;


    public BankRecords(String gender, String area, String marriage, String SaveAccount, String CurrentAccount, String HouseBill, String pepp, int minors, double paycheck, String identification, int years)
    {
        this.sex = gender;
        this.region = area;
        this.married = marriage;
        this.save_act = SaveAccount;
        this.current_act = CurrentAccount;
        this.mortgage = HouseBill;
        this.pep = pepp;
        this.children = minors;
        this.income = paycheck;
        this.id = identification;
        this.age = years;
    }
    public String getId() 
    {
        return id;
    }
    public void setId(String id)
    {
        this.id = id;
    }
    public String getSex()
    {
        return sex;
    }
    public void setSex(String sex)
    {
        this.sex = sex;
    }
    public String getRegion()
    {
        return region;
    }
    public void setRegion(String region)
    {
        this.region = region;
    }
    public String getMarried() 
    {
        return married;
    }
    public void setMarried(String married)
    {
        this.married = married;
    }
    public String getSave_act()
    {
        return save_act;
    }
    public void setSave_act(String save_act)
    {
        this.save_act = save_act;
    }
    public String getCurrent_act() 
    {
        return current_act;
    }
    public void setCurrent_act(String current_act)
    {
        this.current_act = current_act;
    }
    public String getMortgage() 
    {
        return mortgage;
    }
    public void setMortgage(String mortgage) 
    {
        this.mortgage = mortgage;
    }
    public String getPep()
    {
        return pep;
    }
    public void setPep(String pep)
    {
        this.pep = pep;
    }
    public int getAge()
    {
        return age;
    }
    public void setAge(int age) 
    {
        this.age = age;
    }
    public int getChildren()
    {
        return children;
    }
    public void setChildren(int children)
    {
        this.children = children;
    }
    public double getIncome() 
    {
        return income;
    }
    public void setIncome(double income)
    {
        this.income = income;
    }
}

The Client abstract class

 import java.io.*;
    import java.util.*;
    public abstract class Client
    {
        static ArrayList<List<String>> BankArray = new ArrayList<>(25); 
            static BankRecords robjs[] = new BankRecords[600]; 
        public static void readData()
        {
            try
            {
              BufferedReader br;
              String filepath = "C:\\Users\\eclipse-workspace\\Bank_Account\\src\\bank-Detail.csv";
                     br = new BufferedReader(new FileReader  (new File(filepath)));
                    String line;   
                    while ((line = br.readLine()) != null)
                    {
                                  BankArray.add(Arrays.asList(line.split(",")));
                    }   
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            processData();
        }
            public static void processData()
            {
                     int idx=0;
      for (List<String> rowData: BankArray) 
                        {
    robjs[idx] = new BankRecords(null, null, null, null, null, null, null, idx, idx, null, idx);
        robjs[idx].setId(rowData.get(0));
            robjs[idx].setAge(Integer.parseInt(rowData.get(1))); 
                            idx++;
                      }
                 printData(); 
                }
            
    public static void printData() 
    {
        System.out.println("ID\tAGE\tSEX\tREGION\tINCOME\tMORTGAGE");
            int final_record = 24;
            for (int i = 0; i < final_record; i++)
            {
                System.out.println(BankArray.get(i) + "\t ");
            }
        }
    }

The BankRecordsTest class (extends Client)

  import java.util.*;
    import java.io.*;
    public class BankRecordsTest extends Client
    {
        public static void main(String args [])
        {
            readData();
        }
    }

The error

And here is the error.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at java.util.Arrays$ArrayList.get(Unknown Source)
    at Client.processData(Client.java:33)
    at Client.readData(Client.java:24)
    at BankRecordsTest.main(BankRecordsTest.java:7)

I'm not sure what the index problem is. Do note that if you run the ReadData() and PrintData() functions separately, the code runs fine but the ProcessData() method causes issues.

  • 2
    Java naming conventions have classes start with an upper case letter; methods and variables start with lower case letters. This makes it easier to read a Java file. – NomadMaker Oct 25 '20 at 05:12
  • 1
    I'd recommend editing this so all the files are in their own block to make it more readable as well. Currently the line number in the stack trace aren't very useful – William Hammond Oct 25 '20 at 05:45

1 Answers1

0

I think your data is likely not clean and you are making assumptions about the length of your array. The error you are getting stems from this line:

        robjs[idx].setAge(Integer.parseInt(rowData.get(1))); 

Clearly, rowData doesn't have 2 items (or more). This is why you are getting ArrayIndexOutOfBoundsException. So you want to check where your variable was initialized. You quickly realize it comes from

for (List<String> rowData: BankArray)

So then, the following question is where BankArray gets initialized. That happens in 2 places. First of all

    static ArrayList<List<String>> BankArray = new ArrayList<>(25); 

You are creating an empty list. So far so good. Note that you don't need to (and therefore shouldn't) initialize with a size. Lists are not like arrays insofar as they can easily grow and you don't need to give their size upfront.

The second place is

BankArray.add(Arrays.asList(line.split(",")));

This is likely where the issue comes from. Your row variable contains the results of Arrays.asList(line.split(",")). So the size of that list depends on the number of commas in that string you are reading. If you don't have any commas, then the size will be 1 (the value of the string itself). And that's what leads me to concluding you have a data quality issue.

What you should really do is add a check in your for (List<String> rowData: BankArray) loop. If for instance, you expect 2 fields, you could write something along the lines of:

if (rowData.size()<2){
    throw new Exception("hmmm there's been a kerfuffle');
}

HTH

David Brossard
  • 13,584
  • 6
  • 55
  • 88