Hi, so I am trying to read in a text file and store it in a ArrayList in another class.
My reader code below.
public static ArrayList<String> readFile(String fileName) throws Exception { ArrayList<String> data = new ArrayList<String>(); BufferedReader br = new BufferedReader(new FileReader(fileName)); String line = br.readLine(); while (line != null) { String[] values = line.split(","); data.add(line); line = br.readLine(); } br.close(); return data; }
This is the Club class that has the ArrayList of Member.
Below is the Member class showing the Member constructor.
public Member(String data) { String[] list = data.split(","); memberNumber = list[0]; memberName = list[1]; feesPayed = list[2]; sportPlayed = list[3]; } public Member(String memberNo, String name, String fees, String sport) { this.memberNumber = memberNo; this.memberName = name; this.feesPayed = fees; //(need to parse this as a Boolean but haven't managed to do this yet) this.sportPlayed = sport; }
The textfile contains...
123,Sally,true,Tennis
202,Fred,true
313,Siew,true,Tennis
414,Ryan,true,Squash
515,David,false
616,Matthew,running
When I run the code through the UI it comes up with the error of java.lang.ArrayIndexOutOfBoundsException: 3? What have I done wrong?