Odd issue with scope in nested for loops

Im afraid my issue maybe a simple one but I have been working at this for a while to no avail.

Im creating a program that will catch keyboard input, then convert the key into binary and save the binary string to an arraylist. Then Im just trying to display that data. I think my problem is with my nested for loops. I usually try to not go more than 2-3 levels deep but I seems like i need 4 levels for what I am doing. My thought is I have two loops for rows, and columns, another loop for grabbing each element of the arraylist, and one more loop to grab each char of the string of binary. Unfortunately it just displays the last key pressed only. I also had some weird scope issue with the varible k in one of my for loops. It tells me "cannon find k" but I initialize it in the beginning of the for loop. If I initialize k at the top of the code it seems to work.

ArrayList msg = new ArrayList();
Character letter = new Character(' ');
PShape ledOff;
PShape ledOn;
String tempString;
String newString;
int x,y;
int k;
boolean drawn;
void setup() {
  size(640, 360);
  drawn = false;
  x = 20;
  y = 20;
  tempString = "10000000";
  newString = "";
  ledOff = loadShape("off.svg");
  ledOn = loadShape("on.svg");

void draw(){
    for(int ii=0; ii<4; ii++){
      for(int jj=0; jj<18; jj++){
        for(k=0; k<msg.size()-1; k++);{
          tempString = msg.get(k).toString();
          //println("Temp String = "+tempString);
          for(int j=0; j<8;j++){
              shape(ledOn, ii*160+(20*j), jj*20,20,20);
            } else {
              shape(ledOff, ii*160+(20*j), jj*20,20,20);
             // println("off");
            drawn = true;

void keyPressed(){
  drawn = false;
  Character letter = new Character(key);
  newString = binary(letter,8);


  • Answer ✓

    Found my problem.

    for(k=0; k<msg.size()-1;k++);{

    Guess I had been looking at the code for too long, and started chasing a red herring of "Cannot find anything named k"

    Im starting to think maybe I should approach this from an object oriented angle.

Sign In or Register to comment.