Results 1 to 2 of 2
- 11-11-2012, 02:16 PM #1
Member
- Join Date
- Jan 2012
- Location
- Aberdeen
- Posts
- 5
- Rep Power
- 0
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); } }
- 11-11-2012, 04:51 PM #2
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Read in from file and sort in array
By tuts73 in forum New To JavaReplies: 5Last Post: 03-31-2012, 08:25 PM -
read a csv file in an array list
By dellacpa in forum New To JavaReplies: 21Last Post: 06-24-2011, 07:52 PM -
need to read information from file into array
By sadeem in forum New To JavaReplies: 8Last Post: 05-15-2011, 10:00 PM -
Help needed: read array from file and then use it
By ChicagoAve in forum New To JavaReplies: 11Last Post: 02-18-2011, 01:02 PM -
Read File into 2d array
By almjodla in forum New To JavaReplies: 8Last Post: 03-23-2010, 02:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks