# How to ADD or Subtract two values from key board and show result in same window?

**URL:** https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302
**Category:** Coding Questions
**Created:** [January 8, 2019, 6:40am UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302 "2019-01-08T06:40:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Towhid](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Towhid](https://discourse.processing.org/u/Towhid)
#### Post date: [January 8, 2019, 6:40am UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/1 "2019-01-08T06:40:01Z")

</div>

![subtract%20float%20values](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cd1d34bd5979c42a464826d3a474f9992ff55541.jpeg)  
Hi,  
Here I attach screenshot of my processing project. Here I subtract 2 values by putting in my code.  
but i want to take those values from keyboard after run the program and showing results in that place.  
MY CODE is

float a=12.6;  
float b=8.3;  
float d;  
String E ="";  
String F ="";  
String G ="";  
void setup(){  
size(600,300);  
}

void draw(){  
background(200);  
fill(100);

textSize(20);

d = a - b;  
String F =“a=”+a;  
text(F,30,40);

String G =“b=”+b;  
text(G,30,70);

String E =“Result is :” +d;  
text(E,30,95);  
}

please give me the code for Add or Subtract values from taken keyboard and how to show result.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 8, 2019, 7:35am UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/2 "2019-01-08T07:35:35Z")

</div>

as you posted under p5.js:

nice way could be to make little input windows for each value  
and calc the result?

> **[p5.js Web Editor](https://editor.p5js.org/kll/sketches/SJckM0ZGV)**
>
> A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.

for processing JAVA mode  
most easy way is a

> javax.swing.JOptionPane  
> little popup windows ask for text input

but also need to change from text to numbers,  
and i like to use buttons to show the numbers and ask for the numbers on click  
" button ask numbers "

```auto
import javax.swing.JOptionPane;

int N = 0, Y = 69;
float A, B, C, D = 0.0;

color B_border = color(255); // white
color B_fillG = color(0, 255, 0); // green
color B_fillB = color(0, 0, 255); // blue
color B_fillY = color(255, 255, 0); // yellow
color B_text = color(255, 0, 0); // red
int B_textsize=15;

// _________________________________________________________________ Button Menu
void show_Parameter_Buttonmenu() {
  textAlign(CENTER);
  textSize(15);
  strokeWeight(1);
  stroke(255); // white
  line(480, 0, 480, 384); // for button area

  //show_button(int xpos, int ypos, int xwide, int ywide, String Btxt, color border, color filler, color texter)
  show_button(485, 5, 73, 20, "A:"+A, B_border, B_fillG, B_text );
  show_button(485, 30, 73, 20, "B:"+B, B_border, B_fillG, B_text );
  show_button(485, 55, 73, 20, "C:"+C, B_border, B_fillG, B_text );
  show_button(485, 80, 73, 20, "D:"+D, B_border, B_fillG, B_text );
  show_button(485, 105, 73, 20, "N:"+N, B_border, B_fillB, B_text );
  show_button(485, 130, 73, Y, "dY:"+Y, B_border, B_fillY, B_text );
}

// _________________________________________________________________ SETUP
void setup() {
  size(562, 340);
}

// _________________________________________________________________ DRAW
void draw() {
  background(200);   
  ambient(80); //lights(); // static settings
  textAlign(LEFT);
  textSize(20);
  text("Z = "+A+" + "+B+" * X + "+C+" * XX + "+D+" * XXX ", 20, 25); // float
  text("N = "+N, 20, 45); // int
  show_Parameter_Buttonmenu();
}

// _________________________________________________________________ diag print only
void mouseReleased() {
  if (mouseButton == LEFT) { 
    print("LEFT: "); 
    if (overRect(485, 5, 73, 20)) A = askF("A", A);
    if (overRect(485, 30, 73, 20)) B = askF("B", B);
    if (overRect(485, 55, 73, 20)) C = askF("C", C);
    if (overRect(485, 80, 73, 20)) D = askF("D", D);
    if (overRect(485, 105, 73, 20)) N = askI("N", N);
    if (overRect(485, 130, 73, Y)) Y = askI("dY", Y);
  }
  if (mouseButton == RIGHT) { 
    print("RIGHT: ");
  }
  if (mouseButton == CENTER) { 
    print("WHEEL: ");
  }
}

// tab common

// for button menu ask setpoint input
// _________________________________________________________________ askI call: A = askI("A",A);
int askI(String ask, int I) {
  String r = JOptionPane.showInputDialog(null, "new Setpoint for "+ask+" (now "+I+" )", "Input (INT)", JOptionPane.QUESTION_MESSAGE);
  if (r == null ) { 
    print(" NULL "); 
    r = str(I);
  } // handle CANCEL
  try { 
    I = Integer.parseInt(r);
  } 
  catch(NumberFormatException e) { 
    println("you did not enter a int number!");
  }
  println("new "+ask, I);
  return I;
}

// _________________________________________________________________ askF call: A = askF("A",A);
float askF(String ask, float F) {
  String r = JOptionPane.showInputDialog(null, "new Setpoint for "+ask+" (now "+F+" )", "Input (FLOAT)", JOptionPane.QUESTION_MESSAGE);
  if (r == null ) { 
    print(" NULL "); 
    r = str(F);
  } // handle CANCEL
  try { 
    F = Float.parseFloat(r);
  } 
  catch(NumberFormatException e) { 
    println("you did not enter a int or float number!");
  }
  println("new "+ask, F);
  return F;
}

// _________________________________________________________________ mouse position over rectangle yes/no
boolean overRect(int x, int y, int width, int height) {
  if (mouseX >= x && mouseX <= x+width && 
    mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

// _________________________________________________________________ show_button
void show_button(int xpos, int ypos, int xwide, int ywide, String Btxt, color border, color filler, color texter) {
  textAlign(CENTER);
  textSize(B_textsize);
  strokeWeight(2); // Button border width
  stroke(border); // Button border color
  fill(filler); // Button fill color
  rect(xpos, ypos, xwide, ywide); // Button rectangle
  fill(texter); // text color
  text(Btxt, xpos + xwide/2, ypos+ywide/2+B_textsize/2); // some pix too low
  noStroke();
}

```

---

<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 8, 2019, 7:48am UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/3 "2019-01-08T07:48:13Z")

</div>

I can give a few suggestions that should lead you on how to do this instead.

1. Get familiar with `void keyPressed()`  
[https://processing.org/reference/keyPressed\_.html](https://processing.org/reference/keyPressed_.html)
2. Get familiar with `key` variable and use it inside of `keyPressed()`  
[https://processing.org/reference/key.html](https://processing.org/reference/key.html)
3. Have a global `String input1;` or something, and have `keyPressed()` add to it.  
Use `if` statements to determine if `key` is a number, or a key like “S” - if it’s “S”, then switch `keyPressed()` to adding to `String input2` instead using a global `boolean` variable.
4. Use `float()` to convert your `String` to `float`  
[https://processing.org/reference/floatconvert\_.html](https://processing.org/reference/floatconvert_.html)  
You can use it to then do `d = float(input1) - float(input2);

Here, hopefully you’ll be able to figure it out!

---

<div class="post-metadata">

### Author: ![Towhid](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Towhid](https://discourse.processing.org/u/Towhid)
#### Post date: [January 8, 2019, 11:08am UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/4 "2019-01-08T11:08:51Z")

</div>

Boss, is it possible give correction by edit my processing code? If same values i can put by keyboard and get results In same appearance. it will be more easy to understand for me.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 8, 2019, 11:18am UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/5 "2019-01-08T11:18:36Z")

</div>

?? who you are talking to?  
if you address a other user you say  
@kll  
so a msg ( or even email ) pops up in my user panel

did you run my code ? with the buttons and windows come up and you can put in numbers  
and then that numbers are visible at the buttons and can be used elsewhere in the canvas by text()

if you like THAT you use it and cut it down to 2 float buttons  
( deleting much more easy as write again )  
and calculate the result and show it in a text line ( like the text(“Z”… )  
 ![2019-01-08_18-24-29_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c8f94648c36703a5cc0e8b398f0b39c9cd69a5ba.png)

if i do that for you, you might even understand less.

---

<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 8, 2019, 11:28pm UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/6 "2019-01-08T23:28:52Z")

</div>

Not in realtime - as soon as you press “Run”, it’s compiled and values that you “set in stone” will be like that - the only way to change them is by code(which you want), or by re-running the sketch again.

I’m not including different debugging tools Processing offers, because if you’ll start relying on them, these might limit your sketch to being usable only within Processing and only with these tools, which is something best to avoid.

---

<div class="post-metadata">

### Author: ![Towhid](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Towhid](https://discourse.processing.org/u/Towhid)
#### Post date: [January 10, 2019, 5:14pm UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/7 "2019-01-10T17:14:48Z")

</div>

@kll, after editing your code which you were providing me i came to that stage attaching the screenshot here, but I can’t get the result of the sum. Please tell me how I can get the result of the summation. ![ADD%20or%20SUBTRACT](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4f2d42e7f03074891c6aa50a7ee26c0baaf2d156.jpeg)

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 10, 2019, 5:26pm UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/8 "2019-01-10T17:26:29Z")

</div>

you not even need to make a variable for this, unless you want use it further?

```auto
  text("d1 = "+A+" + "+B+" = "+(A+B), 20, 25); // float    
  text("d2 = "+A+" - "+B+" = "+(A-B), 20, 45); // float    

```

 ![2019-01-11_00-33-16_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3472b466da9f616c388a316d5f18060407f0e899.png)

here a example how i used that:  
[http://kll.engineering-news.org/kllfusion01/downloads/RPI\_processing34\_418.jpg](http://kll.engineering-news.org/kllfusion01/downloads/RPI_processing34_418.jpg)

---

<div class="post-metadata">

### Author: ![Towhid](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Towhid](https://discourse.processing.org/u/Towhid)
#### Post date: [January 11, 2019, 12:49pm UTC](https://discourse.processing.org/t/how-to-add-or-subtract-two-values-from-key-board-and-show-result-in-same-window/7302/9 "2019-01-11T12:49:43Z")

</div>

Thanks a lot to your response. finally I get the result.
