Local variable x may not have been initialized

I’m completely new to processing, i’m making a calculator. Please help me!


boolean selected = false;
float x;
float y; {
x = 0;
y = 0; }

void setup() {
    smooth(8);
 size(350,450);
 selected = true;  
}


void draw() {
  noStroke();
  noLoop();
 background(0);
 
 fill(181,183,183);
 ellipse(50,160,50,50);
 ellipse(125,160,50,50);
  ellipse(200,160,50,50);
  
  fill(255,142,0);
    ellipse(275,160,50,50);
    
    fill(32,30,28);
     ellipse(50,235,50,50);
     
         fill(32,30,28);
     ellipse(125,235,50,50);
     
         fill(32,30,28);
     ellipse(200,235,50,50);
     
         fill(255,142,0);
     ellipse(275,235,50,50);
     
         fill(32,30,28);
     ellipse(50,310,50,50);
     
         fill(32,30,28);
     ellipse(125,310,50,50);
     
         fill(32,30,28);
     ellipse(200,310,50,50);
     
         fill(255,142,0);//259
     ellipse(275,310,50,50);
     
         fill(32,30,28);
     ellipse(50,385,50,50);
     
         fill(32,30,28);
     rect(50,360,75,50);
     
         fill(32,30,28);
     ellipse(125,385,50,50);
     
              fill(255,142,0);
     ellipse(201,385,50,50);
     
              fill(255,142,0);
     ellipse(276,385,50,50);
 
 
   fill(0);
 text("NO_INPUT",65646,787867569);
 textSize(30);
 
  fill(0);
 text("7",41,173);
 textSize(30);
 
  fill(0);
 text("8",116,173);
 textSize(30);
 
   fill(0);
 text("9",191,173);
 textSize(30);
 
 fill(255);
  text("/",266,173);
 textSize(30);
 
  fill(255);
  text("4", 41,248);
 textSize(30);
 
   fill(255);
  text("5", 116,248);
 textSize(30);
 
    fill(255);
  text("6", 191,248);
 textSize(30);
 
     fill(255);
  text("X", 266,248);
 textSize(30);

  fill(255);
  text("1", 41,323);
 textSize(30);
 
   fill(255);
  text("2", 116,323);
 textSize(30);
 
    fill(255);
  text("3", 191,323); //305
 textSize(30);
 
     fill(255);
  text("-", 266,323);
 textSize(30);
 
 
   fill(255);
  text("0", 41,398);
 textSize(30);
 
   fill(255);
  text("+", 191,398);
 textSize(30);
 
    fill(255);
  text("=", 266,398); //16,18
 textSize(30);
 
}



void mousePressed()  {
 if(mousePressed) {
     
     if(mouseX > 250 && mouseY > 380) {
    print("=");

  }
    if(mousePressed) {
      
     if(mouseX > 175 && mouseY > 380) {
       print("+"); }
       
           if(mousePressed) {
      
     if(mouseX > 25 && mouseY > 380) {
       print("0");
    
   }
              if(mousePressed) {
      
     if(mouseX > 250 && mouseY > 305) {
       print("-"); 
     String data = "-";
   print(x - y);}
       
                     if(mousePressed) {
      
     if(mouseX > 175 && mouseY > 305) {
       print("3"); 
       float x = 3;
     }

                            if(mousePressed) {
      
     if(mouseX > 125 && mouseY > 305) {
       print("2"); 
     float x = x + 2;
   }
       
         if(mousePressed) {
      
     if(mouseX > 25 && mouseY > 305) {
       print("1");
       float x = x + 1;
       selected = true;
       if(mousePressed) {
         if(mouseX > 25 && mouseY > 305) {
       if(selected = true) {
         float y = y + 1;}

   }
   }
 }
}}}}}}}}
1 Like

This won’t work

Say float x=0;

And leave away the brackets

3 Likes
  1. float x = x + 2; -> x += 2;
  2. float x = x + 1; -> ++x;
  3. float y = y + 1; -> ++y;
2 Likes

Those brackets form an initialization block. And it is valid Java syntax. :coffee:

Although I’d just go w/ float x, y. Which already initializes both fields w/ 0.0f. :stuck_out_tongue:

2 Likes

Still doesn’t work :confused:

Can you please post your entire code

1 Like

Well, in order to be a little bit more readable I have formatted a little bit your code:

boolean selected = false;
float x=0;
float y=0;

void setup() {
 smooth(8);
 size(350,450);
 selected = true;  
 textSize(30);
 textAlign(CENTER);
}


void draw() {
  noStroke();
  noLoop();
  background(0);
 
 fill(181,183,183);
  ellipse(50,160,50,50);
  ellipse(125,160,50,50);
  ellipse(200,160,50,50);
      
 fill(32,30,28);
  ellipse(50,235,50,50);  
  ellipse(125,235,50,50);
  ellipse(200,235,50,50);
  ellipse(50,310,50,50);
  ellipse(125,310,50,50);
  ellipse(200,310,50,50);
  ellipse(50,385,50,50);
  rect(50,360,75,50);
  ellipse(125,385,50,50);
     
 fill(255,142,0);//259
  ellipse(275,160,50,50);
  ellipse(275,235,50,50);
  ellipse(275,310,50,50);
  ellipse(201,385,50,50);
  ellipse(276,385,50,50);
 
 
 fill(0);
 text("NO_INPUT", 150,30);//,65646,787867569);
 text("7",50,160+10);
 text("8",125,160+10);
 text("9",200,160+10);
 
 fill(255);
 text("/",275,160+10);
  text("4", 50,235+10);
  text("5", 125,235+10);
  text("6", 200,235+10);
  text("X", 275,235+10);
  text("1", 50,310+10);
  text("2", 125,310+10);
  text("3", 200,310+10); //305
  text("-", 275,310+10);
  text("0", 50,385+10);
  text("+", 200,385+10);
  text("=", 275,385+10); //16,18 
}



void mousePressed()  {
  if(mouseX > 250 && mouseY > 380) {
    print("=");   }  
  if(mouseX > 175 && mouseY > 380) {
    print("+");   }
  if(mouseX > 25 && mouseY > 380) {
    print("0");   }
  if(mouseX > 250 && mouseY > 305) {
    print("-"); 
    String data = "-";
    print(x - y); }
  if(mouseX > 175 && mouseY > 305) {
    print("3"); 
    x = 3;        }
  if(mouseX > 125 && mouseY > 305) {
    print("2"); 
    x += 2;       }
  if(mouseX > 25 && mouseY > 305) {
    print("1");
     x ++;
    selected = true;
    if(mouseX > 25 && mouseY > 305) {
      if(selected == true) {
          y ++; }
     }
   }
 }

@FilDomInteractive Still doesn’t work :confused:

What doesn’t work? :sweat_smile: :sweat_smile:
There are a lot of things that I suppose are not working, but go in order… for example, this is important and I fixed in the code; use:

if(variable == 0)... (here you are comparing the value of the variable)
instead of
if(variable = 0)... ( here you are giving a new value)

I haven’t check very in detail, but you had several times “float x = x + 1” or similar things, if you want to give x a new value, don’t use float in front (you are creating a new variable) use “x = x +1;” or what is better “x++;” or “x +=1;”

And finishing by now, if you want to use the “buttons” you have to specify the area better; example:

     if(mouseX > 25 &&  mouseX < 75 && mouseY > 305 && mouseY < 355) 

and for free; better to use " if {…}else if{…}else if{…}… " to spend less time comparing “ifs” if you want just one case of them.

Hope this helps you, and you can fix more things :smiley:

See you :rotating_light:

3 Likes

that’s for 33-3 =

and 33 + 3 =

(The other buttons don’t work yet)

Chrisir

boolean selected = false;
int x=0;
int y=0;

int bufferNum=0;

String command="";

void setup() {
  size(350, 450);

  smooth(8);
  selected = true;  
  textSize(30);
  textAlign(CENTER);
}

void draw() {
  background(0);

  noStroke();
  // noLoop();
  showCalc();
}

// ---------------------------------------------------

void showCalc() {

  //fill(181, 183, 183);
  fill(32, 30, 28);
  ellipse(50, 160, 50, 50);
  ellipse(125, 160, 50, 50);
  ellipse(200, 160, 50, 50);

  fill(32, 30, 28);
  ellipse(50, 235, 50, 50);  
  ellipse(125, 235, 50, 50);
  ellipse(200, 235, 50, 50);
  ellipse(50, 310, 50, 50);
  ellipse(125, 310, 50, 50);
  ellipse(200, 310, 50, 50);
  ellipse(50, 385, 50, 50);
  rect(50, 360, 75, 50);
  ellipse(125, 385, 50, 50);

  fill(255, 142, 0);//259
  ellipse(275, 160, 50, 50);
  ellipse(275, 235, 50, 50);
  ellipse(275, 310, 50, 50);
  ellipse(201, 385, 50, 50);
  ellipse(276, 385, 50, 50);


  fill(0);
  fill(255);
  text("Calculator: try 33-3=", 150, 30);//,65646,787867569);

  textAlign(RIGHT);
  text(bufferNum, width-30, 90);//,65646,787867569);
  textAlign(CENTER); // restore default 

  fill(255);
  text("7", 50, 160+10);
  text("8", 125, 160+10);
  text("9", 200, 160+10);

  fill(255);
  text("/", 275, 160+10);
  text("4", 50, 235+10);
  text("5", 125, 235+10);
  text("6", 200, 235+10);
  text("x", 275, 235+10-2);
  text("1", 50, 310+10);
  text("2", 125, 310+10);
  text("3", 200, 310+10); //305
  text("-", 275, 310+10);
  text("0", 50, 385+10);
  text("+", 200, 385+10);
  text("=", 275, 385+10); //16,18
}

void mousePressed() {
  if (mouseX > 250 && mouseY > 380) {
    print("=");
    y=bufferNum;
    // eval command 
    switch(command) {
    case "+":
      bufferNum=x+y;
      break; 

    case "-":
      bufferNum=x-y;
      break;

    case "/":
      //
      break; 

    case "x":
      //
      break;
    }//switch 
    x=0;
    y=0;
  } else if (mouseX > 175 && mouseY > 380) {
    print("+");
    x=bufferNum; 
    y=0;
    bufferNum=0;
    command = "+";
  } else  if (mouseX > 25 && mouseY > 380) {
    print("0");
  } else if (mouseX > 250 && mouseY > 305) {
    print("-"); 
    x=bufferNum; 
    y=0;
    bufferNum=0;
    command = "-";
    //print(x - y);
  } else if (mouseX > 175 && mouseY > 305) {
    println("3***"); 
    bufferNum = extend (bufferNum, "3");
  } else if (mouseX > 125 && mouseY > 305) {
    print("2"); 
    x += 2;
  } else  if (mouseX > 25 && mouseY > 305) {
    print("1");
    x ++;
    selected = true;
    if (mouseX > 25 && mouseY > 305) {
      if (selected == true) {
        y ++;
      }
    }
  }
}

int extend(int what_, String add_) {
  String buffer1= trim(str(what_)) + add_;
  int buffer2 =int(buffer1);
  println(buffer1);
  return buffer2;
} 
//
3 Likes

Additionally:

object oriented programming

You might want to look into object oriented programming. Thus you would have a new type (a class) Button that “represents” one button. Then you could have a list (array) of your buttons and for loop over it. Cool.

Each button would have its position (x and y), its text (“8”) and its command ("+", “-”…) but also functions like display() or mouseIsOver() all in one package.

That would make positioning and checking if the mouse is on the button much easier.

Please see https://www.processing.org/tutorials/objects/

checking the mouse

For checking mouse you could also use dist() (instead of if(mouseX<....&&mouseY<...) )

if(dist(buttonX,buttonY, mouseX,mouseY) < 20) {
     ....
}

Chrisir

1 Like