Results 1 to 12 of 12
  1. #1
    deeevo is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Reading in a text file, constructing and adding it to an ArrayList

    I'm reading in the following text file.

    Tennis,44,10,93,10,11,12,13,14,15,16
    Squash,165,10,27,1,2,3,4

    I have the error - java.lang.ArrayIndexOutOfBoundsException: 1 - this is obviously due to the size of the elements and I haven't declared 10, 11... etc and 1, 2... in the Sport class yet because I'm not to sure on how too. These numbers are Court Numbers and in the Sport class I have an ArrayList of Court - although I don't know how to initialise this in my Sport class and the constructor.

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

    Java Code:
    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?!
        }
    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?

    Java Code:
        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];
            }
        }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    On a List (ArrayList) you have to invoke the add() method. So that would probably look more like
    Java Code:
    courtList.add( list[4] );
    If your class has a courtList field, then you must not declare it anywhere else. Wrong example:
    Java Code:
    public class Sport {
       private List<Court> courtList;
    
       public Sport(String data) {
          List<Court> courtList = new ArrayList<>(); <----- this variable is now local to the constructor, and the field is still null.
       }
    }
    Correct example:
    Java Code:
    public class Sport {
       private List<Court> courtList;
    
       public Sport(String data) {
          courtList = new ArrayList<>();
       }
    }

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    On second thought, the ArrayLlist is supposed to hold Court objects, so writing: courtList.add( list[4]) doens't make sense. That would be something like this:
    Java Code:
    Court court = new Court();
    //apply settings here to court or via constructor
    courtList.add( court );
    Last edited by SurfMan; 04-25-2013 at 08:48 AM. Reason: Better example

  4. #4
    deeevo is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    1. I have the add method in the class where the collection is held.
    Java Code:
     public Club()
        {
            try
            {
                membersList = new ArrayList<Member>();
                getMemberFile();
                sportsList = new ArrayList<Sport>(); 
                getSportFile();
            }
            catch(Throwable ex)
            {
                System.out.println(ex);
            }
        }
    
    ....etc 
    
    
        public void getSportFile() throws Exception
        {
            ArrayList<String> sportData = FileOperations.readFile("SportData.txt");
            
            for(String s : sportData)
            {
                sportsList.add(new Sport(s));
            }
        }
    2. Yeah sorry I wasn't too sure on how to declare the ArrayList in the constructor, does the "courtList = new ArrayList<>();" go in the constructor or where the text is read through into the Sport class - public Sport(String data)

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    If courtList is a field, you can initialize it in several places:
    1) While declaring the field
    2) The constructor of Sport
    3) Other methods

    This mostly depends on your personal preference, and sometimes has a technical reason. In your case I'd go with option 2, since that will be the most logical option for you, IMHO.

    Option 1:
    Java Code:
    public class Sport {
       private List<Court> courtList = new ArrayList<>();
     
       public Sport(String data) {
    
       }
    }
    Option 2:
    Java Code:
    public class Sport {
       private List<Court> courtList;
     
       public Sport(String data) {
          courtList = new ArrayList<>();
       }
    }
    Option 3:
    Java Code:
    public class Sport {
       private List<Court> courtList;
     
       public Sport(String data) {
          fillData(data);
       }
    
       private void fillData(String data) {
          courtList = new ArrayList<>();
          ....
       }
    }

  6. #6
    deeevo is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    It's now printing to the terminal - finally :)

    The output is below:

    Sport: Tennis, 44.0, 93.0, 10.0
    Sport: Squash, 165.0, 27.0, 10.0

    I obviously need to change my toString() method to print the ArrayList of court. Because it's a collection of an identifier (if that even makes sense, and not singular (such as the others) how would I write this to display it. I currently have courtList but this just adds [] on to the end of the '10.0' to symbolise an array is present, but with no values?

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    I can't really tell without looking at some code. I took my crystal ball to the local witch for maintenance :)

  8. #8
    deeevo is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    Ha, my bad. So it's reading obviously from the ouput I sent earlier. But I'm not sure if it's separating the court numbers into their individual indexes. I'm getting no error, so I assume it's just a displaying option.

    Java Code:
    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]);
            courtList = new ArrayList<>();          
        }
        
        /**
         * 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;
        }
    
    ....etc
    
    // I will be changing this... but it's only to see output first then I will. 
        public String toString()
        {
            return "Sport: " + sportName + ", " + usageFee + ", " + affiliationFee+ ", " + insuranceFee + courtList;
        }

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    You still need to make the Court objects and add them to the courtList.

  10. #10
    deeevo is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    So there is no Court objects in the courtList arrayList which is why they're not showing when printed to the terminal? A Court object also has a collection of Bookings, so I'm not able to create an object without this. However, I was wondering if there is a way to show the court numbers, for instance... the project is based on a booking scheme to book courts for sports. So a member rings up and they want to book a court, is it possible to show what courts are available when there has been no bookings (eg the system has been deployed today so there would be no bookings but you want to find out what courts there are - if that even makes sense)?

    I ultimately need to beable to add, delete and block a booking... aswell as show the bookings for a member and the bookings for a court.

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    What you might want to do is to first create all the model/domain classes. Put the information in the class where it belongs. Then if classes need other classes, make sure they have a list or an instance of that object. Then think about which object creates those other objects. Does the Sport class create instances of Courts? Possibly. Does a Court create a Booking? I doubt it. Try to put the responsibilities where they belong.

  12. #12
    deeevo is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Reading in a text file, constructing and adding it to an ArrayList

    Quote Originally Posted by SurfMan View Post
    What you might want to do is to first create all the model/domain classes. Put the information in the class where it belongs. Then if classes need other classes, make sure they have a list or an instance of that object. Then think about which object creates those other objects. Does the Sport class create instances of Courts? Possibly. Does a Court create a Booking? I doubt it. Try to put the responsibilities where they belong.

    I've created all the classes needed (although I am trying to work out which is the best way to implement the date and time in the booking class) we were pretty much just given a link to copy from, but there's no point putting it in your code if you don't understand. "Each sport is allocated a number of courts" and "A court has a 0 or more booking" so from a booking it's assigned to a court which we were told Court having a collection of Bookings.

Similar Threads

  1. Replies: 7
    Last Post: 03-19-2013, 01:58 AM
  2. Reading from txt file to arraylist
    By artur in forum New To Java
    Replies: 4
    Last Post: 02-10-2012, 04:51 AM
  3. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  4. Reading from file and adding to JTable
    By purple in forum New To Java
    Replies: 11
    Last Post: 04-14-2011, 10:33 PM
  5. Problem with constructing an ArrayList
    By Diplo in forum New To Java
    Replies: 5
    Last Post: 12-07-2009, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •