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


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.

float a=12.6;
float b=8.3;
float d;
String E ="";
String F ="";
String G ="";
void setup(){

void draw(){


d = a - b;
String F =“a=”+a;

String G =“b=”+b;

String E =“Result is :” +d;

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

Thanks in advance.


as you posted under p5.js:

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

for processing JAVA mode
most easy way is a

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 "

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() {
  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() {
  ambient(80);   //lights();                                   // static settings
  text("Z = "+A+" + "+B+" * X + "+C+" * XX + "+D+" * XXX ", 20, 25);                // float
  text("N = "+N, 20, 45);                                                           // int

//_________________________________________________________________ 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) {
  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


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

  1. Get familiar with void keyPressed()
  2. Get familiar with key variable and use it inside of keyPressed()
  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
    You can use it to then do `d = float(input1) - float(input2);

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


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.


?? who you are talking to?
if you address a other user you say
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”… )

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


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.


@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


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

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

here a example how i used that:


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