0

This is for an assignment, I'm not asking for answer answers/code to help me out but a point in the right direction would help me greatly.

I have approximately 250 lines of data (olypics data) that I want to load into a constructor and then make an array of said object.

The data about olympic games is just a basic 5 column setup of:

Year, Nation, Gold, Silver, Bronze
1992,Unified_Team,45,38,29
1992,United_States,37,34,37
1992,Germany,33,21,28
1992,China,16,22,16
1992,Cuba,14,6,11                   <<--output from excel into csv format

and I want to load said data into my constructor

 public POlympics(int year, String nation, int gold, int silver, int bronze) 
    {
        this.year = year;
        this.nation = nation;
        this.gold = gold;
        this.silver = silver;
        this.bronze = bronze;
    }

My problem is, the data if output in csv format...must be loaded as strings into the array (whereas I want the year,gold,silver,bronze as integers for basic calculations in other methods).

What are some simpler ways to load my data into the constructor + array? (without having to make them all strings, then convert them all to ints or floats, and lose marks for it). Alternatives to CSV format which mean I won't have to mess about with Integer.parseInt etc

Cheers :)

Edited by mike_2000_17: Fixed formatting

1
Contributor
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Teckiwi
0
/*
 * OlympicReader.java
 * 
 * Created on 16/05/2008, 14:48:07
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package polympics;

import java.io.*;
/**
 *
 * @author Teckiwi
 */
public class OlympicReader {

    public OlympicReader() {
    }
   //POlympics(int year, String nation, int gold, int silver, int bronze)
    public void load(POlympics[] data)
    {
        int i = 0;
        int year;
        String nation;
        int gold;
        int silver;
        int bronze;
        String line;
        try {
            BufferedReader in = new BufferedReader(new FileReader("Data2read.csv"));
            while ((line = in.readLine())!=null) {
                String[] fields = line.split(",");
                year=Integer.parseInt(fields[0]); 
                nation=fields[1]; 
                gold=Integer.parseInt(fields[2]); 
                silver=Integer.parseInt(fields[3]); 
                bronze=Integer.parseInt(fields[4]);
                data[i]=new POlympics(year, nation, gold, silver, bronze);
                i++;
            }
            }   catch (IOException e) {
                System.out.println("There was a problem with the file");
                e.printStackTrace();
            }   //catch
    }

}

added the parseInt's to my loader class, but a non csv way would be more handy

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.