0

I am trying to instantiate an array of objects (runners) by reading them in from a file. There are six different variables that are descriptive of each person. I am trying to figure out how to create an array that stores all the same values of these runners within the same element, while maintaining the individual pieces of each runner.

the file looks like this:

{1,Gebre Gebremariam,2:08:00,,Ethiopia,ETH
2,Emmanuel Mutai,2:06:28,,Kenya,KEN
3,Geoffrey Mutai,2:05:06,,Kenya,KEN
4,Tsegaye Kebede,2:07:14,,Ethiopia,ETH
6,Jaouad Gharib,2:08:26,,Morocco,MAR
7,Meb Keflezighi,2:09:13,CA,United States,USA
8,Mathew Kisorio,2:10:58,,Kenya,KEN
10,Viktor Rothlin,2:12:26,,Switzerland,SUI
11,Bobby Curtis,2:16:44,PA,United States,USA
12,Ed Moran,2:11:47,VA,United States,USA
14,Abdellah Falil,2:10:35,,Morocco,MAR
15,Juan Luis Barrios,2:14:10,,Mexico,MEX
18,Stephen Muzhingi,2:29:10,,Zimbabwe,ZIM}
Wes Johnson
  • 69
  • 1
  • 1
  • 7

2 Answers2

1

Create a class Runner which has properties for the six fields that make up the individual runner, and then read your file into a List<Runner> or an Runner[].

// if this is a CSV file
List<Runner> runners = new ArrayList<Runner>();
for (String[] line: csvLines){
   Runner r = new Runner();
   r.setName(line[0]);
   r.setAge(Integer.parseInt(line[1]);
   runners.add(r);
}
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Here is a list of CSV libraries for Java: http://stackoverflow.com/questions/101100/csv-api-for-java – Thilo Jan 25 '13 at 03:57
0

The following adds an instance of Runner to an ArrayList. You may want to parse the string to provide the appropriate constructor arguments.

List<Runner> runners = new ArrayList<Runner>();

BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String currentLine;

for(int index = 0; (currentLine = bufferedReader.readLine()) != null; index++)
{
    runners.add(new Runner(currentLine));
}

bufferedReader.close();
Zach Latta
  • 3,251
  • 5
  • 26
  • 40