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 7 of 7

Thread: Java - reading in two text files of different sizes 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 Java - reading in two text files of different sizes into an ArrayList

    I'm reading in my second text file of data, it is listed below. My first text file is reading and printing to the terminal fine.

    // this is a comment, ignore
    // name, usagefee, insurance, affiliationfees, then court numbers
    Tennis,44,10,93,10,11,12,13,14,15,16
    Squash,165,10,27,1,2,3,4

    I am using the same buffer/reader as for my first text file, which is listed 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;
        }

    However, obviously a different readFile method. Which is below...

        public Club()
        {
            try
            {
                membersList = new ArrayList<Member>();
                getMemberFile();
                sportsList = new ArrayList<Sport>(); 
                getSportFile();
            }
            catch(Throwable ex)
            {
                System.out.println(ex);
            }
        }
     
        public void getSportFile() throws Exception
        {
            ArrayList<String> sportData = FileOperations.readFile("SportData.txt");
     
            for(String s : sportData)
            {
                sportsList.add(new Sport(s));
            }
        }

    Here is the Sport class - with the constructor (which isn't complete yet as I am know too sure on how to deal with the different lengths of data of the court numbers)

    public class Sport
    {
        private String sportName;
        private double usageFee;
        private double insuranceFee;
        private double affiliationFee;
        private ArrayList<Court> courtsList;
     
        public Sport(String data)
        {
            String[] list = data.split(",");
     
            sportName = list[0];
            usageFee = Double.parseDouble(list[1]);
            insuranceFee = Double.parseDouble(list[2]);
            affiliationFee = Double.parseDouble(list[3]);
    // haven't added the variables for the courts yet because I'm not too sure how to go about it.
    // same as in the constructor
        }
     
     // Sport constructor
        public Sport(String nameOfSport, double uFee, double iFee, double aFee)
        {
           this.sportName = nameOfSport;
           this.usageFee = uFee;
           this.insuranceFee = iFee;
           this.affiliationFee = aFee;
        }

    I have a few problems. I have an ArrayList of courts however I am not too sure of the next step in using this in my constructor, due to be a different type and varying in length of the data.

    I don't think it's parsing right or if I am even going in the right means about converting it to a 'double' for the fees. I can't even tell if it's working anyway because it's not printing to the terminal. I am receiving an error - java.lang.NumberFormatException: For input string: "ignore". Which is probably due to the above reasons.

    If someone is able to please point me in the right direction. I don't expect to be given the exact answer, just a bit guidance and/or example in a different context.


  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: Java - reading in two text files of different sizes into an ArrayList

    java.lang.NumberFormatException: For input string: "ignore".

    Your reader doesn't know comments, it's also reading the comments in the file. Either you ignore comment lines before parsing, or you have to remove them from the file.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java - reading in two text files of different sizes into an ArrayList

    java.lang.NumberFormatException: For input string: "ignore"
    What data is in the file being read? Where does the String: "ignore" come from?
    Can the code detect the line with the invalid numeric data and skip it to prevent the exception?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java - reading in two text files of different sizes into an ArrayList

    Well that was silly... I just removed it from the text file - the comments.

    I now have the error java.lang.ArrayIndexOutOfBoundsException: 1, which in my understanding is referring to the first line of the text file due and the court numbers.

    1. How to declare the ArrayList of Court into the Constructor of Sport?
    2. Then I would need a check to implement the varied lengths of data of the court numbers at the different elements of the arraylist?
    3. I am not sure if the double values of the fees are parsing from string to double because I can't see the output, but am I going about this the right way?

    Cheers.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java - reading in two text files of different sizes into an ArrayList

    java.lang.ArrayIndexOutOfBoundsException: 1
    The code tries to use an index value that is past the end of the array. The array has less than two elements.
    Remember that array indexes range in value from 0 to the array length-1
    Find the array that is being referenced and see why the code is using an index past the end of the array.

    You can use the array's .length attribute to check the length of the array and not index it with values past the end. If length = 1, then max index is 0
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java - reading in two text files of different sizes into an ArrayList

    The idea I have in my head of what I am trying to do is...

    public class Sport
    {
        private String sportName;
        private double usageFee;
        private double insuranceFee;
        private double affiliationFee;
        private ArrayList<Court> courtList;
     
     
        public Sport(String data)
        {
            String[] list = data.split(",");
     
            sportName = list[0];
            usageFee = Double.parseDouble(list[1]);
            insuranceFee = Double.parseDouble(list[2]);
            affiliationFee = Double.parseDouble(list[3]);
    // I want to store 10 for the Tennis element and 1 for Squash at the fourth index list[4] etc..
    // 10= list[4]
    //         11= list[5]
    //         12= list[6]
    //         13= list[7]
    //         14 = list[8]...etc
    // but then this wouldn't work for the squash court data
        }
     
        /**
         * Constructor for objects of class Sport
         */
        public Sport(String nameOfSport, double uFee, double iFee, double aFee)
        {
           this.sportName = nameOfSport;
           this.usageFee = uFee;
           this.insuranceFee = iFee;
           this.affiliationFee = aFee;
           ArrayList<Court> courtList = new ArrayList<Court>(); //not sure if this is right, I've being passed the data for the courts, so wouldn't this need to be also in the parameter for Sport constructor - confused?!
        }

    However, I think I know what your trying to say in your reply... I've never used ArrayLists as you can tell nor have we learnt properly about them. But there must be an easier way from the idea of how I think it should be - or I could be completely wrong.

    --- Update ---

    Something like this... if the elements index is greater than 7 (for Tennis) courtList equals all of those indexes. Else... if its less it assigns (which it is for Squash) it assigns it, its values. Expressions are probably wrong, but is this the idea?

        public Sport(String data)
        {
            String[] list = data.split(",");
     
            sportName = list[0];
            usageFee = Double.parseDouble(list[1]);
            insuranceFee = Double.parseDouble(list[2]);
            affiliationFee = Double.parseDouble(list[3]);
            if(list.length > 7){           
                courtList = list[4];
                courtList = list[5];
                courtList = list[6];
                courtList = list[7];
                courtList = list[8];
                courtList = list[9];
            }else{
                courtList = list[4];
                courtList = list[5];
                courtList = list[6];
                courtList = list[7];
            }
        }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java - reading in two text files of different sizes into an ArrayList

    Also posted at Reading in a text file, constructing and adding it to an ArrayList
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Help - reading from a text file into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 07:47 AM
  2. 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
  3. 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
  4. Reading text files and the calculating averages from it
    By tuffguy721 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 30th, 2013, 05:18 PM
  5. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM

Tags for this Thread