We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! I having trouble getting my code to print correctly. I understand that since it is an external csv data file, its hard to tell what I am talking about specifically- my apologies. My project is data visualization, and in my csv file I have two columns- one for countries and the other for the biomass indicator. What is happening is that when I run my code, is that the second half of the data set does not print all numbers. It is supposed to print "In Argentina there is a biomass of 16000000" for about 110 different countries, except the number, which is declared by "tons" in the code below. However, not all the numbers will be printed, and will instead be replaced by 0. This occurs randomly throughout the code, and I cannot seem to find a pattern, other than that maybe there is some limit thats converting numbers of certain lengths to 0. Can anyone help?
// 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
int x;
int y;
int h;
int w;
// constructor splits on "," and puts pieces into data fields
Item(String country, int tons) {
this.country = country;
this.tons = tons;
this.x = 0;
this.y = 0;
this.h = 0;
this.w = 0;
}
String toString() {
String msg = "In " + country;
msg += " there is a biomass of " + tons + " tons";
return(msg);
}
void display() {
}
}
Answers
A Java
int
type got a positive range of 2^31 - 1. A little more than 2.1 billions.Access MAX_INT to know its exact threshold value:
println(MAX_INT);
If the biomass exceeds MAX_INT, int() is gonna fail.
Another reason is trailing spaces and other invisible characters.
Clean that up w/ trim():
int( trim(pieces[1]) )
https://Processing.org/reference/trim_.html
You should also consider loadTable() in place of loadStrings():
https://Processing.org/reference/loadTable_.html
Alright thanks a ton I will try this
continued here: https://forum.processing.org/two/discussion/16336/data-visualization