# Calculator plus sign will not show on the display screen, Any ideas?

**URL:** <https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790>\
**Category:** Coding Questions\
**Created:** [April 15, 2020, 12:19am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790 "2020-04-15T00:19:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![WeeBoEtte](https://avatars.discourse-cdn.com/v4/letter/w/e47c2d/32.png) [@WeeBoEtte](https://discourse.processing.org/u/WeeBoEtte)\
**Post date:** [April 15, 2020, 12:19am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790/1 "2020-04-15T00:19:50Z")

</div>

Hello, I am trying to make a simple calculator in processing, but when I click the plus sign, it does not display, instead, a little box displays. Any ideas?

Calculator tab

```auto
Keypad keyPad;

color backgroundC = color(200);
String entery_text="Calculator";
float firstNumber;
boolean isReadingFirstNumber =false;
char operation;
float secondNumber;

void setup(){
  size(400, 500);
    background(backgroundC);
  noStroke();
  textSize(60);
  
  keyPad = new Keypad();
}

void draw(){

  keyPad.Display();
  text(firstNumber,10,70);
  text(operation,10,70);
 
}
void mousePressed(){
  if( mouseX > 10 && mouseX < 80){
    if(mouseY > 80 && mouseX < 300){
    
   firstNumber=1;
   if(isReadingFirstNumber){
     firstNumber=1;
   }else{
     secondNumber= 1;
     
if( mouseY > 80 && mouseY < 400){
if(mouseX > 80 && mouseX < 310){
   operation = '+';

    
  }
  }
}
  }
}
}

```

Keypad tab

```auto
class Keypad {
color backgroundC = color(200);
color entryC = color(242,244,255);
color entryTC = color(0);
color buttonC = color(255);
color buttonTC = color(93, 67, 247);
float firstNumber;

  Keypad() {
    
  }
    void Display(){
      
      
 //making the background behind the keys gray     
  background(backgroundC);
  
  //making the screen display background slightly purple
  fill(entryC);
  rect(10, 10, 380, 70);
  
  fill(entryTC);

  
  //making the background of the button's white
  fill(buttonC);
  
  //The square for 7
  rect(10, 100, 80, 80, 20);
  
  //the square for 8
  rect(110, 100, 80, 80, 20);
  
  //the square for 9
  rect(210, 100, 80, 80, 20);
  
    //the square for /
  rect(310, 100, 80, 80, 20);
  
    //the square for 4
  rect(10, 200, 80, 80, 20);
  
    //the square for 5
  rect(110, 200, 80, 80, 20);
  
    //the square for 6
  rect(210, 200, 80, 80, 20);
  
    //the square for x
  rect(310, 200, 80, 80, 20);
  
    //the square for 1
  rect(10, 300, 80, 80, 20);
  
    //the square for 2
  rect(110, 300, 80, 80, 20);
  
    //the square for 3
  rect(210, 300, 80, 80, 20);
  
    //the square for -
  rect(310, 300, 80, 80, 20);
  
    //the square for C
  rect(10, 400, 80, 80, 20);
  
    //the square for 0
  rect(110, 400, 80, 80, 20);
  
    //the square for =
  rect(210, 400, 80, 80, 20);
  
    //the square for +
  rect(310, 400, 80, 80, 20);
  
  fill(buttonTC);
  
  text("7", 27, 162);
  text("8", 127, 162);
  text("9", 227, 162);
  text("/", 333, 155);
  
  text("4", 27, 262);
  text("5", 127, 262);
  text("6", 227, 262);
  text("x", 327, 262);
  
  text("1", 27, 362);
  text("2", 127, 362);
  text("3", 227, 362);
  text("-", 327, 362);
  
  text("C", 27, 462);
  text("0", 127, 462);
  text("=", 227, 462);
  text("+", 327, 462);

    

}

void mousePressed(){
  if( mouseX > 10 && mouseX < 80){
    if(mouseY > 300 && mouseX < 30){
    
  
   firstNumber=1;
    
  }

  
}
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 15, 2020, 12:32am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790/2 "2020-04-15T00:32:13Z")

</div>

Hello,

This displayed correctly (no box):

```auto
char operation = ' '; // Was not initialized to a chat that can be displayed and defaulted to 0

```

I did not explore further than that.

`:)`

---

<div class="post-metadata">

**Author:** ![WeeBoEtte](https://avatars.discourse-cdn.com/v4/letter/w/e47c2d/32.png) [@WeeBoEtte](https://discourse.processing.org/u/WeeBoEtte)\
**Post date:** [April 15, 2020, 12:42am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790/3 "2020-04-15T00:42:51Z")

</div>

I did that, but it gave me a null pointer exception when I put this code in draw: text(operation,200,70);

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 15, 2020, 12:53am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790/4 "2020-04-15T00:53:50Z")

</div>

Hello,

I can only get a 1 displayed.  
With the character initialized I do not gt a “box”.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a9a73172284a87d74371751a9a496c71e8f51229.png)

That is all I got.

`:)`

---

<div class="post-metadata">

**Author:** ![WeeBoEtte](https://avatars.discourse-cdn.com/v4/letter/w/e47c2d/32.png) [@WeeBoEtte](https://discourse.processing.org/u/WeeBoEtte)\
**Post date:** [April 15, 2020, 12:54am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790/5 "2020-04-15T00:54:57Z")

</div>

Gotcha! thanks for letting me know!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 15, 2020, 12:56am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790/6 "2020-04-15T00:56:54Z")

</div>

And this:

```auto
char operation; //displays box
//char operation = ' '; // displays "blank"
//char operation = '+'; // displays +
void setup()
  {
  textSize(48);
  textAlign(CENTER, CENTER);
  text(operation, width/2, height/2-10);
  }

```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0471323632d24b39f4749276eadb669b4082adf4.png)

Ok that is it for now.

`:)`

---

<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:** [April 15, 2020, 8:31am UTC](https://discourse.processing.org/t/calculator-plus-sign-will-not-show-on-the-display-screen-any-ideas/19790/7 "2020-04-15T08:31:16Z")

</div>

Hello,

there are other difficulties in your code

(Again, mousePressed() could be simplified when you had a list of buttons (objects, see other thread). You could just for-loop over the list of buttons. Depending on its **type** you would then treat them differently:

- type number: just add the number of the button to the current number (at its end) - for ALL number buttons the same
- type operator: fill the text of the button in the operator variable. For ±\*/
- type equal : calc the result based on two numbers and operator and display it
- type Clear: clear everything  
)

1. 

The mousePressed() function in the class is not called automatically by processing.

You would have to call it from mousePressed() outside the class.

* * *

1. 

When you hit **ctrl-t** in processing you get auto-format: better indents. See below: Your code after ctrl-t.

Here you see that the closing brackets } are often wrong.

E.g. this

```auto
      if (isReadingFirstNumber) {
        firstNumber=1;
      } else {
        secondNumber= 1;

```

should close directly.

1. 

Also this

```auto
  if ( mouseX > 10 && mouseX < 80) {
    if (mouseY > 80 && mouseX < 300) {

```

should **close** prior to this

```auto
        if ( mouseY > 80 && mouseY < 400) {
          if (mouseX > 80 && mouseX < 310) {

```

1. 

```
  firstNumber=1;
  if (isReadingFirstNumber) {

```

You should not set firstNumber outside the if. It would get overwritten by 1 when the second number comes in! Bad.

1. 

```auto
  if ( mouseX > 10 && mouseX < 80) {
    if (mouseY > 80 && mouseX < 300) {

```

you check mouseX, mouseX and then mouseY but _then again mouseX_! That’s wrong. Also the numbers are wrong:

```auto
  if ( mouseX > 10 && mouseX < 10+80) {
    if (mouseY > 300 && mouseY < 300+80) {

```

and you don’t need a double if, you can work with **&&** :

```auto
  if (mouseX > 10 && mouseX < 10+80 && 
     mouseY > 300 && mouseY < 300+80) {

```

1. 

before setup you had `boolean isReadingFirstNumber = false;`

must be

```auto

boolean isReadingFirstNumber = true;

```

You are reading the first number at startup.

1. 

Also, in draw() you display firstNumber. You should have a variable textToDisplay there and fill it, because you also need to display secondNumber later.

**Your code after ctrl-t**

```auto
void mousePressed() {

  if ( mouseX > 10 && mouseX < 80) {
    if (mouseY > 80 && mouseX < 300) {

      firstNumber=1;
      if (isReadingFirstNumber) {
        firstNumber=1;
      } else {
        secondNumber= 1;

        if ( mouseY > 80 && mouseY < 400) {
          if (mouseX > 80 && mouseX < 310) {
            operation = '+';
          }
        }
      }
    }
  }
}

```

**My code of the function mousePressed()**

```auto

void mousePressed() {
  if ( mouseX > 10 && mouseX < 10+80 && 
    mouseY > 300 && mouseY < 300+80) {
    println("1");
    if (isReadingFirstNumber) {
      firstNumber=1;
    } else {
      secondNumber= 1;
    }
  }

  if (mouseX > 310 && mouseX < 310+80 && 
    mouseY > 400 && mouseY < 400+80) {
    println("+");
    operation = '+';
    isReadingFirstNumber=false;
  }
  //
}

```

Warm regards,

Chrisir

**Full entire Sketch**

```auto

Keypad keyPad;

color backgroundC = color(200);

String entery_text="Calculator";

float firstNumber = 0;
float secondNumber = 0;
boolean isReadingFirstNumber = true; // !!!!!!!!!!!!!!!!!!!
char operation='?'; // !!!!!!!!!!!!!!!!!!!
float textToDisplay = firstNumber; // !!!!!!!!!!!!!!!!!!!

void setup() {
  size(1400, 500);
  background(backgroundC);
  noStroke();
  textSize(60);
  keyPad = new Keypad();
}

void draw() {
  keyPad.Display();
  text(textToDisplay, 40, 70);
  text(operation, 10, 70);
  text(firstNumber+" "+ operation+" " + secondNumber, 440, 70); // !!!!!!!!!!!!!!!!!!!
}

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

void mousePressed() {
  //
  if ( mouseX > 10 && mouseX < 10+80 && 
    mouseY > 300 && mouseY < 300+80) {
    println("1");
    if (isReadingFirstNumber) {
      firstNumber=1;
      textToDisplay = firstNumber; // !!!!!!!!!!!!!!!!!!!
    } else {
      secondNumber= 1;
      textToDisplay = secondNumber; // !!!!!!!!!!!!!!!!!!!
    }
  }

  if (mouseX > 310 && mouseX < 310+80 && 
    mouseY > 400 && mouseY < 400+80) {
    println("+");
    operation = '+';
    isReadingFirstNumber=false;
    textToDisplay = secondNumber; // !!!!!!!!!!!!!!!!!!!
  }
  //
}

// ===============================================================

class Keypad {
  color backgroundC = color(200);
  color entryC = color(242, 244, 255);
  color entryTC = color(0);
  color buttonC = color(255);
  color buttonTC = color(93, 67, 247);
  float firstNumber;

  Keypad() {
    // empty constructor
  }

  void Display() {

    //making the background behind the keys gray     
    background(backgroundC);

    //making the screen display background slightly purple
    fill(entryC);
    rect(10, 10, 380, 70);

    fill(entryTC);

    //making the background of the button's white
    fill(buttonC);

    //The square for 7
    rect(10, 100, 80, 80, 20);

    //the square for 8
    rect(110, 100, 80, 80, 20);

    //the square for 9
    rect(210, 100, 80, 80, 20);

    //the square for /
    rect(310, 100, 80, 80, 20);

    //the square for 4
    rect(10, 200, 80, 80, 20);

    //the square for 5
    rect(110, 200, 80, 80, 20);

    //the square for 6
    rect(210, 200, 80, 80, 20);

    //the square for x
    rect(310, 200, 80, 80, 20);

    //the square for 1
    rect(10, 300, 80, 80, 20);

    //the square for 2
    rect(110, 300, 80, 80, 20);

    //the square for 3
    rect(210, 300, 80, 80, 20);

    //the square for -
    rect(310, 300, 80, 80, 20);

    //the square for C
    rect(10, 400, 80, 80, 20);

    //the square for 0
    rect(110, 400, 80, 80, 20);

    //the square for =
    rect(210, 400, 80, 80, 20);

    //the square for +
    rect(310, 400, 80, 80, 20);

    fill(buttonTC);

    text("7", 27, 162);
    text("8", 127, 162);
    text("9", 227, 162);
    text("/", 333, 155);

    text("4", 27, 262);
    text("5", 127, 262);
    text("6", 227, 262);
    text("x", 327, 262);

    text("1", 27, 362);
    text("2", 127, 362);
    text("3", 227, 362);
    text("-", 327, 362);

    text("C", 27, 462);
    text("0", 127, 462);
    text("=", 227, 462);
    text("+", 327, 462);
  }
  //
}//class

```
