# Local variable x may not have been initialized

**URL:** https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477
**Category:** Beginners
**Created:** [October 6, 2019, 6:41pm UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477 "2019-10-06T18:41:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![FilDomInteractive](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fildominteractive/32/6678_2.png) [@FilDomInteractive](https://discourse.processing.org/u/FilDomInteractive)
#### Post date: [October 6, 2019, 6:41pm UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/1 "2019-10-06T18:41:47Z")

</div>

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

* * *

```auto
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;}

   }
   }
 }
}}}}}}}}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 6, 2019, 6:52pm UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/2 "2019-10-06T18:52:52Z")

</div>

> [@FilDomInteractive](#):
>
> float x;  
> float y; {  
> x = 0;  
> y = 0; }

This won’t work

Say float x=0;

And leave away the brackets

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 6, 2019, 6:58pm UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/3 "2019-10-06T18:58:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 6, 2019, 10:05pm UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/4 "2019-10-06T22:05:10Z")

</div>

> [@Chrisir](#):
>
> And leave away the brackets.

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

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

---

<div class="post-metadata">

### Author: ![FilDomInteractive](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fildominteractive/32/6678_2.png) [@FilDomInteractive](https://discourse.processing.org/u/FilDomInteractive)
#### Post date: [October 7, 2019, 12:09am UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/5 "2019-10-07T00:09:51Z")

</div>

Still doesn’t work 😕

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 7, 2019, 7:59am UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/6 "2019-10-07T07:59:00Z")

</div>

Can you please post your entire code

---

<div class="post-metadata">

### Author: ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)
#### Post date: [October 7, 2019, 8:57am UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/7 "2019-10-07T08:57:06Z")

</div>

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

```auto
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 😕

What doesn’t work? 😅 😅  
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:

```auto
     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 😃

See you 🚨

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 7, 2019, 11:34am UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/8 "2019-10-07T11:34:40Z")

</div>

that’s for 33-3 =

and 33 + 3 =

(The other buttons don’t work yet)

Chrisir

```auto
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;
} 
//

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 9, 2019, 1:57pm UTC](https://discourse.processing.org/t/local-variable-x-may-not-have-been-initialized/14477/9 "2019-10-09T13:57:22Z")

</div>

**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/](https://www.processing.org/tutorials/objects/)

**checking the mouse**

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

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

```

Chrisir
