We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
}
}
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
https://Processing.org/reference/loadTable_.html
fixed the coding with the first link, but the link at the bottom I've used before and it worked, however my professor wants us to use this format with a class declared, which is where things get tricky
Line 25. You're using a constant for your index for data.
Oh okay, could you explain a little further?
Should I be using i?
Okay I changed it to i, and i get the countries to read correctly, but the numbers are a little mixed up. I can't find a pattern because it will only print some numbers in the second half of the split string and not all.
We don't have the data and you haven't given us enough details. 'Some numbers' isn't enough.
You say it splits on ',' but the one line you give us has a | between the country and the tonnage
continued here: https://forum.processing.org/two/discussion/16336/data-visualization