Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help - reading from a text file into an ArrayList

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help - reading from a text file into an ArrayList

    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.
        public Club()
        {
            try
            {
                membersList = new ArrayList<Member>();
                getFile();
            }
            catch(Throwable ex)
            {
                System.out.println(ex);
            }
        }
     
     -----
     
        public void getFile() throws Exception
        {
            ArrayList<String> data = FileOperations.readFile("MemberData.txt");
     
            for(String s : data)
            {
                membersList.add(new Member(s));
            }
        }

    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?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Help - reading from a text file into an ArrayList

    sportPlayed = list[3];
    what is list[3] for this row: 202,Fred,true

    See the mistake?

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Help - reading from a text file into an ArrayList

    If you have the option that a member may not play a sport then you may want to add a check to make sure the data contains a 4th element before referencing it. This would be a good way to have your input not break your code. You need to define statements based on your expected data. If your users must have a declared sport then you should be throwing an error, but if they have the option of not having one then you need to check for it.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    deeevo (April 19th, 2013)

Similar Threads

  1. Reading into an ArrayList from a text file
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 04:32 PM
  2. Reading from a text file into an ArrayList
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 01:24 AM
  3. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  4. Reading a text file into an arraylist of fueldispensers.
    By vbhatti in forum Object Oriented Programming
    Replies: 3
    Last Post: November 20th, 2011, 01:17 PM
  5. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM

Tags for this Thread