I am having an Issue

I am trying to make a bar graph for the following data however my “i” integer is not being recognized.

String[] name = {
“1997:”,
“1998:”,
“1999:”,
“2000:”,
“2001:”,
“2002:”,
“2003:”,
“2004:”,
“2005:”,
“2006:”,
“2007:”,
“2008:”,
“2009:”,
“2010:”,
“2011:”,
“2012:”,
“2013:”,
“2014:”,
“2015:”,
“2016:”,
};

int[] fires = {

6,835 ,
5,227 ,
7,562 ,
5,177 ,
6,223 ,
5,759 ,
5,961 ,
5,574 ,
4,908 ,
4,805 ,
3,610 ,
3,593 ,
2,858 ,
2,434 ,
3,056 ,
2,922 ,
3,672 ,
2,920 ,
3,231 ,
2,816 ,
};

void setup(){
size(500,500);
}

void draw(){
int x =0;
for(int i=0; i<10; i++){

if(mouseX>x && mouseX<=x+40){
  fill(255, 96, 79);
}else{
  fill(255, 36, 13);
}

}

float h = map(fires[i], 2300, 7600, 36, 340);

rect(x+4, height-h, 32, h);

x+=8;
}

I am very new to processing and need some help.

The brace that ends the for loop seems to be in the wrong place. It should probably be after the x += 8 line?

I changed that however when I run it now I get an error “ArrayIndexOutOfBoundsException: 40” with my float h line being highlighted

Hi magic_moose,

You can use name.length and fires.length to find the number of entries you have in each of your arrays. This will help when using a for-loop to run through them. By my count, name has 20 entries while fires has 40. I’m assuming that there’s a connection between the two, like every 2 entries in fires is to be matched with every 1 entry in name?

void setup() {
  size(500, 500);
  println("name's length", name.length);
  println("fires's length", fires.length);

  int minsz = min(fires.length, name.length);
  int relsz = max(fires.length, name.length) / minsz;
  println(relsz);

  for (int i = 0, j = 0; i < minsz; ++i, j += relsz) {
    println(name[i], fires[j], ",", fires[j + 1]);
  }
}

You can work with more than one variable in your for-loop to keep your arrays in sync. In this case, my variable i is matched with the name array, while j is matched with the fires array. Because fires is twice as big, j must be incremented twice as fast as i. This is why I used j += relsz (relative size or length, 2) in the third section of the for-loop. i and j only exist within the for-loop, and disappear from memory after it finishes. minsz and relsz will disappear after setup finishes.

A sample of what this sketch will print in the console:

1997: 6 , 835
1998: 5 , 227
1999: 7 , 562
2000: 5 , 177

I recommend watching Dan Shiffman’s videos, particularly this one on variable scope. Depending on where a variable like float h is located relative to your curly braces, { and }, Processing may not know that it exists or what information it contains.

Hope that helps out!

Thank you for the help however where it comes with 40 outputs it was meant to be 20. the commas in the numbers were just to show a number placement ex: 1,000 = 1000.

Sorry for the confusion, I am just very very lost

Hi magic_moose,

It’s very helpful that you pointed that out. Those commas are confusing Processing, too, because it uses commas to separate entries in an array, not for grouping within a number. So, although this isn’t solving your big problem yet, you can now get rid of a small problem which is making the bigger one harder to solve.

int[] fires = {
  6835, 5227, 7562, 5177, 6223,
  5759, 5961, 5574, 4908, 4805, 
  3610, 3593, 2858, 2434, 3056, 
  2922, 3672, 2920, 3231, 2816
};

int sz = fires.length;
for(int i = 0; i < sz; ++i) {
  println(i + 1, nfc(fires[i]));
}

If in the future I ever want to display the values in fires with commas to make them easier to read I can use nfc. (How I treat my information when I’m working in code is a separate design issue from how I format and display it to other people.)

Figuring out how to draw rectangles on the screen with a for-loop might be another sub-problem which will get you closer to your big goal. Again, Shiffman’s videos are helpful, in my opinion.

Best,