Data string not being read correctly (biomass)

I am new to computer science, and am having trouble loading an outside data file into my code. I understand that it will be hard to tell what my issue is when you can't see the csv file, but I was hoping there was something glaring that I just wasn't seeing.

The problem is this:

I have a csv file of about 239 rows and 2 columns, listing the countries in the first column and the biomass in tons in the second. The first data entry is "Angola |10734000000," and then it goes through the other countries as it goes down. At first, when I printed the code listed below, what came up in the black box at the bottom of the screen is just "In Angola there is a biomass of 0 tons" 239 times. I've changed line 25, and it will now print out all the countries, but only some of the numbers are printed. There does not see to be a pattern in which numbers it chooses to print. All other numbers it automatically fills in with zero.

Additionally I need some guidance in formatting a graph for this data. Thanks in advance.

// parseFile2
/** 
Load a csv file into an array of strings <br>
Construct data objects out of each line and store in array
*/

String[] data;
Item[] items;

void setup() {
  // Load data from a file as array of strings

  data = loadStrings("biomass.csv");

  //create object array

  items = new Item[data.length];

  //fill object array

  for (int i=0; i<data.length; i++) {

    //split each line into pieces on ","

    String[] pieces = data[i].split(",");

    // start 

    items[i] = new Item(pieces[0], int(pieces[1]));
  }

  for (int i=0; i<items.length; i++) {

    println(items[i]);

  }
}

class Item {  

  String country;  // Country name

  int tons;         // biomass in tons


  // constructor splits on "," and puts pieces into data fields

  Item(String country, int tons) {

    this.country = country;

    this.tons = tons;

  }

  String toString() {

    String msg = "In " + country;

   msg += " there is a biomass of " + tons + " tons";

    return(msg);
  }
}
Tagged:

Answers

This discussion has been closed.