Results 1 to 2 of 2
  1. #1
    adize is offline Member
    Join Date
    Jan 2012
    Location
    Aberdeen
    Posts
    5
    Rep Power
    0

    Default Read from file to array - problem

    Hello everyone. I have a coursework at university and my assignment is to make a set of classes for application for euro 2012. I managed to write five classes (abstract Person, Player, Manager, Country, Group) and tested it, everything works fine except one method - I was asked to write a method in Group class to read data from txt file and create Player and Manager objects from it. Person, Player and Manager are simple classes with some attributes like name, age, number of goals etc.

    The Country class consists of two fields String name; Person[] squad. The Group class consists of one field private Country[] teams.

    I know the basic idea how to load data from txt file, but in this particular example, I do not know exactly how to add created objects to the array (especially when it is an array in a second array (squad array in teams array). I would really appreciate your help and suggestions how best to apporach it. Thank you!

    Java Code:
      public void readFromFile(File f){
            try {
                BufferedReader br = new BufferedReader(new FileReader(f));
                String nextLine = br.readLine();
                while(nextLine != null){
                    StringTokenizer st = new StringTokenizer(nextLine,",");
                    int type = Integer.parseInt(st.nextToken());
                    String name1 = st.nextToken();
                    String name2 = st.nextToken();
                    int age = Integer.parseInt(st.nextToken());
    
                    
                    if (type == 1){
                        int squadNo = Integer.parseInt(st.nextToken());
                        int capsNo = Integer.parseInt(st.nextToken());
                        int goalsNo = Integer.parseInt(st.nextToken());
                        boolean ifCaptain = Boolean.parseBoolean(st.nextToken());
                        String position = st.nextToken();
                        Player p = new Player(name1, name2, age, squadNo, position, 
                                capsNo, goalsNo, ifCaptain);
                        //add person to array;
                    } else if (type == 2){
                        int yearStart = Integer.parseInt(st.nextToken());
                        Manager m = new Manager(name1, name2, age, yearStart);
                       //add manager to array;
                    } 
                    nextLine = br.readLine();
                }
                br.close();
            } catch (Exception ex) {
                System.out.println(ex);
                System.exit(1);
            }
        }

  2. #2
    kuriozal is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Read from file to array - problem

    Just don't use array. Use some kind of Collection. ArrayList will be good in this case.

    Then just use add method.

Similar Threads

  1. Read in from file and sort in array
    By tuts73 in forum New To Java
    Replies: 5
    Last Post: 03-31-2012, 08:25 PM
  2. read a csv file in an array list
    By dellacpa in forum New To Java
    Replies: 21
    Last Post: 06-24-2011, 07:52 PM
  3. need to read information from file into array
    By sadeem in forum New To Java
    Replies: 8
    Last Post: 05-15-2011, 10:00 PM
  4. Help needed: read array from file and then use it
    By ChicagoAve in forum New To Java
    Replies: 11
    Last Post: 02-18-2011, 01:02 PM
  5. Read File into 2d array
    By almjodla in forum New To Java
    Replies: 8
    Last Post: 03-23-2010, 02:55 PM

Tags for this Thread

Posting Permissions

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