# Basic calculator for android

**URL:** <https://discourse.processing.org/t/basic-calculator-for-android/7172>\
**Category:** Coding Questions\
**Created:** [January 4, 2019, 4:26pm UTC](https://discourse.processing.org/t/basic-calculator-for-android/7172 "2019-01-04T16:26:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bossofeng](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bossofeng](https://discourse.processing.org/u/bossofeng)\
**Post date:** [January 4, 2019, 4:26pm UTC](https://discourse.processing.org/t/basic-calculator-for-android/7172/1 "2019-01-04T16:26:52Z")

</div>

I have a basic problem i write a basic calculator it works but i pressed then equal symbol if i press a new number first answer stay there and new answer write up last answer my english not very well so i will upload a photo i hope you can help me

 ![Screenshot_20190104-191946](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7429530e776c1eb37487a22482e03c5394bd69a0.png)

And my codes:

```auto
String input = "";

float input1 = 0;
float input2 = 0;

int operator = 0;

void setup() {

  background(255);
  size(720,1280);
  
  fill(95, 158, 160);
 
  rect(0, height/5, width, height);

  textSize(height/10);
  frameRate(90);
  fill(255);
  text("1", 20, height/4+20, width, height);
  text("2", 20+175, height/4+20, width, height);
  text("3", 20+175+175, height/4+20, width, height);
  text("+", 20+175+175+175, height/4+20, width, height);

  text("4", 20, height/4+255, width, height);
  text("5", 20+175, height/4+255, width, height);
  text("6", 20+175+175, height/4+255, width, height);
  text("-", 20+175+175+175, height/4+255, width, height);

  text("7", 20, height/4+255+255, width, height);
  text("8", 20+175, height/4+255+255, width, height);
  text("9", 20+175+175, height/4+255+255, width, height);
  text("x", 20+175+175+175, height/4+255+255, width, height);

  text("C", 20, height/4+255+255+255, width, height);
  text("0", 20+175, height/4+255+255+255, width, height);
  text("=", 20+175+175, height/4+255+255+255, width, height);
  text("/", 20+175+175+175, height/4+255+255+255, width, height);
}
void draw() { //ekran
  fill(220,20,60);
  text(input, 30, 150);

}

void mousePressed() {
 
  if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4 && mouseY<= height/4+255 ) {
    input = input + 1;

  } else if (mouseX >= 175 && mouseX <= 0+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +2;
  
  } else if (mouseX >= 0+175+175 && mouseX <= 0+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +3;
   
  } else if (mouseX >= 0+175+175+175 && mouseX <= 0+175+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    println("+");
    input1 = parseInt(input);
    input = "";
    input = input + "+";
    operator = 1;
  noStroke();
    fill(255);
    rect(0, 0, width, height/4);
    // rad 2
  } else if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4+255 && mouseY<= height/4+255+255) {
    input = input +4;
  
  } else if (mouseX >=175 && mouseX <= 175+175 && mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +5;
  
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +6;
  
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    println("-");
    input1 = parseInt(input);
    input = "";
    fill(255);
    rect(0, 0, 667, 212);
    input = input + "-";
    operator = 2;
    

    //rad 3
  } else if (mouseX >= 0 && mouseX <= 175&& mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +7;

  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +8;

  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255&& mouseY<= height/4+255+255+255 ) {
    input = input +9;
  
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    println("x");
    input1 = parseInt(input);
    input = "";
    fill(255);
    rect(0, 0, 667, 212);
    input = input + "";
    operator = 3;

    //rad 4
  } else if (mouseX >= 0 && mouseX <= 0+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("C");
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
    fill(255);
    rect(0, 0, 665, 200);
  
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255+255&& mouseY<= height/4+255+255+255+255+255) {
    input = input +0;
 
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("=");
    input2 = parseInt(input);
    input = input + "=";
   
    fill(255);
    rect(0, 0, 900, 300);
    fill(220,20,60);

    if (operator == 1) {
      text(int(input1 + input2), 30, 150);
      operator = 0;
    } else if (operator == 2) {
      text(int(input1 + input2), 30, 150);
    } else if (operator == 3) {
      text(int(input1 * input2), 30, 150);
    } else if (operator == 4) {
      text(nf(input1 / input2,0,0), 30, 150);
      if (input2 == 0)
;
    }
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    input1 = parseInt(input);
    input = "";
    fill(255);
    rect(0, 0, 667, 212);
    input = input + "";
    operator = 4;
  
  }
  println(input);
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 4, 2019, 4:54pm UTC](https://discourse.processing.org/t/basic-calculator-for-android/7172/2 "2019-01-04T16:54:46Z")

</div>

The problem here is how you draw the things.  
You draw all of the buttons and everything inside of `setup()`, which is run only once, and then you draw just the input text inside of `draw()`. As a result, there is nothing to clear the stuff you draw to the screen, so it just draws ontop of it again and again. In other words: in that image, you have 45 ontop of 71 - what is there to _remove_ 71 before drawing 45?

As a fine fix, I suggest either moving `background(...`, `text(...` and other functions into `draw()` instead. Or, in this case, you can just draw white on the upper part of the screen.

```auto
void draw() { //Экран... Я русский, но форум англоязычный, и чтобы больше людей понимали я на английском. Так что, пожалуйста, отвечайте на английском. :v
fill(255);
rect(0,0,width,height/5);
fill(220,20,60);
text(input, 30, 150);
}

```

This does not fix the problem completely, as the drawn result gets drawn over with that `rect(...` pretty quickly, but it should give you an idea of what to do next to fix it.  
Hints: Use variables, store your result in a variable, add `boolean displayResult` and set it to `true` when the user presses `=`, and use `if(displayResult)` inside of `draw()` to know when to display `input` or when to display `result`! 😃

P.S., 4th and 5th arguments of `text` are used only if your text is really long and needs to move part of it to the next line - your text is 1 characters long and you are giving it huge boundaries, so you can just ignore it.

---

<div class="post-metadata">

**Author:** ![bossofeng](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bossofeng](https://discourse.processing.org/u/bossofeng)\
**Post date:** [January 4, 2019, 5:05pm UTC](https://discourse.processing.org/t/basic-calculator-for-android/7172/3 "2019-01-04T17:05:58Z")

</div>

Thank you but my english not very well can you write this code for me ?because i didnt understand completly.

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 4, 2019, 5:30pm UTC](https://discourse.processing.org/t/basic-calculator-for-android/7172/4 "2019-01-04T17:30:29Z")

</div>

Oh. Here’s the code with my hints and solutions applied:

```auto
String input = "";

float input1 = 0;
float input2 = 0;
float result = 0;

int operator = 0;

boolean drawResult = false;

void setup() {

  background(255);
  size(720, 1280);

  fill(95, 158, 160);

  rect(0, height/5, width, height*4/5);

  textSize(height/10);
  frameRate(15); //frameRate of 90 is unneeded for a calculator and drains the battery! You won't even notice the difference, trust me.
  fill(255);
  text("1", 30, height/5*1.7);
  text("2", 30+175, height/5*1.7);
  text("3", 30+175+175, height/5*1.7);
  text("+", 30+175+175+175, height/5*1.7);

  text("4", 30, height/5*2.7);
  text("5", 30+175, height/5*2.7);
  text("6", 30+175+175, height/5*2.7);
  text("-", 30+175+175+175, height/5*2.7);

  text("7", 30, height/5*3.7);
  text("8", 30+175, height/5*3.7);
  text("9", 30+175+175, height/5*3.7);
  text("x", 30+175+175+175, height/5*3.7);

  text("C", 30, height/5*4.7);
  text("0", 30+175, height/5*4.7);
  text("=", 30+175+175, height/5*4.7);
  text("/", 30+175+175+175, height/5*4.7);
}
void draw() { //ekran
  fill(255);
  rect(0, 0, width, height/5);
  fill(220, 20, 60);
  if (drawResult) {
    text(nfc(result,3), 30, 150);
  } else {
    text(input, 30, 150);
  }
}

void mousePressed() {

  if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4 && mouseY<= height/4+255 ) {
    input = input +1;
    drawResult = false;
  } else if (mouseX >= 175 && mouseX <= 0+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +2;
    drawResult = false;
  } else if (mouseX >= 0+175+175 && mouseX <= 0+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    input = input +3;
    drawResult = false;
  } else if (mouseX >= 0+175+175+175 && mouseX <= 0+175+175+175+175 && mouseY >= height/4 && mouseY<= height/4+255) {
    println("+");
    drawResult = false;
    input1 = parseInt(input);
    input = "+";
    operator = 1;
    //noStroke();
    //fill(255);
    //rect(0, 0, width, height/4);
    // rad 2
  } else if (mouseX >= 0 && mouseX <= 175 && mouseY >= height/4+255 && mouseY<= height/4+255+255) {
    input = input +4;
    drawResult = false;
  } else if (mouseX >=175 && mouseX <= 175+175 && mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +5;
    drawResult = false;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    input = input +6;
    drawResult = false;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175&& mouseY >= height/4+255 && mouseY<= height/4+255+255 ) {
    println("-");
    drawResult = false;
    input1 = parseInt(input);
    input = "-";
    //fill(255);
    //rect(0, 0, 667, 212);
    //input = input + "-";
    operator = 2;

    //rad 3
  } else if (mouseX >= 0 && mouseX <= 175&& mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +7;
    drawResult = false;
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    input = input +8;
    drawResult = false;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255&& mouseY<= height/4+255+255+255 ) {
    input = input +9;
    drawResult = false;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255 && mouseY<= height/4+255+255+255 ) {
    println("x");
    drawResult = false;
    input1 = parseInt(input);
    input = "x";
    //fill(255);
    //rect(0, 0, 667, 212);
    operator = 3;

    //rad 4
  } else if (mouseX >= 0 && mouseX <= 0+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("C");
    drawResult = false;
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
    //fill(255);
    //rect(0, 0, 665, 200);
  } else if (mouseX >= 175 && mouseX <= 175+175 && mouseY >= height/4+255+255+255&& mouseY<= height/4+255+255+255+255+255) {
    input = input +0;
  } else if (mouseX >= 175+175 && mouseX <= 175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    println("=");
    input2 = parseInt(input.substring(1)); //.substring(1) removes 1 symbol from the text
    input = input + "=";

    //fill(255);
    //rect(0, 0, 900, 300);
    //fill(220,20,60);
    if (operator == 1) {
      result = input1 + input2;
    } else if (operator == 2) {
      result = input1 - input2;
    } else if (operator == 3) {
      result = input1 * input2;
    } else if (operator == 4) {
      result = input1 / input2;
    }
    drawResult = true;
    input = "";
    input1 = 0;
    input2 = 0;
    operator = 0;
  } else if (mouseX >= 175+175+175 && mouseX <= 175+175+175+175 && mouseY >= height/4+255+255+255 && mouseY<= height/4+255+255+255+255+255 ) {
    input1 = parseInt(input);
    input = "";
    fill(255);
    //rect(0, 0, 667, 212);
    input = input + "";
    operator = 4;
  }
  println(input);
}

```

Here, fancied the code a little.  
Good luck with coding stuff!

---

<div class="post-metadata">

**Author:** ![bossofeng](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bossofeng](https://discourse.processing.org/u/bossofeng)\
**Post date:** [January 4, 2019, 5:35pm UTC](https://discourse.processing.org/t/basic-calculator-for-android/7172/5 "2019-01-04T17:35:19Z")

</div>

Thank you very much 😀🖑

---

<div class="post-metadata">

**Author:** ![bossofeng](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bossofeng](https://discourse.processing.org/u/bossofeng)\
**Post date:** [January 4, 2019, 10:53pm UTC](https://discourse.processing.org/t/basic-calculator-for-android/7172/6 "2019-01-04T22:53:03Z")

</div>

This time divided button not working how i fix it?
