So I'm parsing a CSV and trying to create a new object from each record. Using BufferedReader != null runs thru the file just fine. Now I use string.split method to put each line into an array. Ideas on how to use the values in the array to create the recordRowObject from the array? I'd post code but I'm on the road mulling this over in my head.

Recommended Answers

All 10 Replies

It all depends on how you want to pass the data to the recordRowObject class. What does each instance of the class contain?
You could use a constructor for example.

There's about 50 different floats, chars, Strings and ints >.>

while((line = br.readLine()) != null ){
    String[] recordArray = line.split(",");

Gives me one row in the array; when I attempt to just pass the Array to the the Object constructor =(

Do you want to keep all as string or else? If they are all string, then in your constructor would be...

String[] rowData;

public recordRowObject(String[] data) {
  if (data==null) { throw new IllegalArgumentException("\nData cannot be null!"); }
  rowData = new String[data.length];
  foreach(String s : data) {
    rowData[i] = s;

Now, if you want a mix type of data, you should know before hand what type relates to what data. You may use ArrayList for dynamic growth. Below is just a sample of how you may do it...

ArrayList<String> rowStringData = new ArrayList<String>();
ArrayList<Character> rowCharData = new ArrayList<Character>();
ArrayList<Float> rowFloatData = new ArrayList<Float>();
ArrayList<Integer> rowIntegerData = new ArrayList<Integer>();

public recordRowObject(String[] data) {
  if (data==null) { throw new IllegalArgumentException("\nData cannot be null!"); }
  foreach(String s : data) {
    if (s.length()==0) { continue; }  // ignore empty string
    else if (s.length()==1) {  // may be a character or integer
      try {  // try integer first
        rowIntegerData.add(new Integer(Integer.parseInt(s)));
      catch (NumberFormatException ex) {  // failed from integer, it is a char
        rowCharData.add(new Character(s.charAt(0)));
      catch (Exception e) {  // unknown exception, catch it
    else {
      // do similar thing to the length of 1 but more varieties here...

Little more complicated than I thought...

It will all depend on how you want your data structure to keep the data and how you are going to use it... You could make it as simple as the first example I gave (copy the string to the class data), but then I don't know how you use the data though...

    public Record(char sumLev, char region, char division, char state, String Name, int census2010population, int estimateBase2010, int popEstimate2010, int popEstimate2011, int nPopChg_2010, int nPopChg_2011, int births2010, int births2011, int deaths2010, int deaths2011, int naturalInc2010, int naturalInc2011, int internationalMig2010, int internationalMig2011, int domesticMig2011, int netMig2010, int netMig2011, int residual2010, float residual2011, float rBirth2011, float rDeath2011, float rNaturalInc2011, float rInternationalMig2011, float rDomesticMig2011, float rNetMig2011) {
        this.sumLev = sumLev;
        this.region = region;
        this.division = division;
        this.state = state;
        this.Name = Name;
        this.census2010population = census2010population;
        this.estimateBase2010 = estimateBase2010;
        this.popEstimate2010 = popEstimate2010;
        this.popEstimate2011 = popEstimate2011;
        this.nPopChg_2010 = nPopChg_2010;
        this.nPopChg_2011 = nPopChg_2011;
        this.births2010 = births2010;
        this.births2011 = births2011;
        this.deaths2010 = deaths2010;
        this.deaths2011 = deaths2011;
        this.naturalInc2010 = naturalInc2010;
        this.naturalInc2011 = naturalInc2011;
        this.internationalMig2010 = internationalMig2010;
        this.internationalMig2011 = internationalMig2011;
        this.domesticMig2011 = domesticMig2011;
        this.netMig2010 = netMig2010;
        this.netMig2011 = netMig2011;
        this.residual2010 = residual2010;
        this.residual2011 = residual2011;
        this.rBirth2011 = rBirth2011;
        this.rDeath2011 = rDeath2011;
        this.rNaturalInc2011 = rNaturalInc2011;
        this.rInternationalMig2011 = rInternationalMig2011;
        this.rDomesticMig2011 = rDomesticMig2011;
        this.rNetMig2011 = rNetMig2011;

The full arg constructor and in some cases the data isn't inherently obvious. Sample row below:

10  0   0   0   United States   308745538   308745538   309330219   311591917   584681  2261698 990000  4008000 595786  2450126 394214  1557874 190467  703824  0   0   190467  703824  0   0   12.90983126 7.891894516 5.017936742 2.267028212 0   2.267028212

Bah...I think I got it. Instead of each row into an array create a new array for each field then prase thru them independently to create objects.

Then it is obvious. You could pass in a string array. Check for a certain size of the array. Then parse each index to whatever data you want.

public Record(String[] data) {
  if (data==null || data.length<30) {
    throw new IllegalArgumentException("\nData array size must be at least 30!");
  try {
    this.sumLev = new Character(data[0].charAt(0));
    this.region = new Character(data[1].charAt(0));
    this.division = new Character(data[2].charAt(0));
    this.state = data[3];  # shouldn't be a char
    this.Name = data[4];
    this.census2010population = Integer.parseInt(data[5]);
    this.rNetMig2011 = Double.parseDouble(data[29]);
  catch (Exception e) {

I'm not going to read all the code above, but this snippet did catch my eye:

String[] rowData;
public recordRowObject(String[] data) {
  if (data==null) { throw new IllegalArgumentException("\nData cannot be null!"); }
  rowData = new String[data.length];
  foreach(String s : data) {
    rowData[i] = s;

actually ... you're taking it a bit too far. also: if you post code (which is clearly not meant as pseudocode, but actual code) try not to mix up languages. there is no 'foreach' in java, it would simply be:

for (String s: data){

but what I was really baffled about, is why you didn't just do:

String[] rowData;
public recordRowObject(String[] data){
  if ( data == null )
    throw new IllegalArgumentException("\nData cannot be null!"); 
  rowData = data;

Oops, I was working with Perl when I answered the post. Sorry >_<

Hmm... Even though String is immutable, what would happen if you resize the passing String array argument outside the class right after you assign it to the class member? That's why I copy it instead of assigning it.

PS: It doesn't matter anyway because the OP wants to save the data in different category and type, so the OP must parse each string to certain data types.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.