Input an integer value

edited February 2015 in How To...

Seems dump but I cannot find a way to input an integer using the keyboard. I tried the following but it doesn't work. int a; if ((keyPressed == true) { a=key; }


  • edited February 2015

    What exactly do you mean when you say it doesn't work?

    Keep in mind that the key variable is a char, which holds the unicode character. A unicode character is just an integer that represents a specific character. 'A' is 65, 'B' is 66, etc.

    In other words, if the user presses the 2 key, key will be the char '2'.

    The unicode value of the '2' character is 50, which is what you're setting i equal to.

    You need a way to map that 49 back to the digit 2. One way to do that is by using the Character class:

    a = Character.digit(key, 10);

    If you have more questions, please post your code in the form of an MCVE, and don't forget to use the code button to preserve formatting.


    • Variables declared in a locally scope cease to exist once that scope ends.
    • If you need other places to see variable a, it needs to be declared globally as a field.
    • Also we don't need to test for true b/c it can be safely omitted:
      if (keyPressed) a = key;
  • Thank you for your response. I understand that key is of char type. I want my program to read an integer value in the setup function. Do I have to use a = Character.digit(key, 10); or I could use a more simple instruction?

  • What do you mean by more simple? That's a one-line solution. You could probably get away with using subtraction, but that seems kludgy to me.

    There are probably hundreds of ways to do this, and google is your friend.

  • edited February 2015

    ... or I could use a more simple instruction?

    Alternative implementation of Character.digit():

    static final int digitToNum(char ch) {
      int num = ch - '0';
      return num >= 0 & num <= 9? num : -1;
  • @GoToLoop That code doesn't use your num variable, so it's not returning the correct value. Please run example code before posting it.

  • edited February 2015

    Oops! Forgot to change ch to num within the return as its output! Fixed now! X_X

  • This shows a dialog asking for a number:

    import javax.swing.JOptionPane;
    int i = 0;
    String r = JOptionPane.showInputDialog(null, "What number?", "Decide", JOptionPane.QUESTION_MESSAGE);
    try {
      i = Integer.parseInt(r);
    } catch(NumberFormatException e) {
      println("you did not enter a number!");
    println("i =", i);
  • @hamoid Grateful for your post, this is what I want, but i get an error message.


  • edited February 2015
    Input to string. 
    Converse string to int.
    in setup
     in draw() 
    check key  0...9
    str += key
    check ENTER to stop input
    convers to int
    clear str
    in keyPressed()
  • edited February 2015

    It shoulda worked for JOptionPane belongs to standard Java API! :-/
    Or are you under Android's modified Java version (Dalvik) by chance? :-?
    Nonetheless, a similar alternative for @hamoid's version: \m/

    import static javax.swing.JOptionPane.*;
    String input = showInputDialog("Type in integer number...");
    int num = parseInt(input == null? "" : input, MIN_INT);
    if (input == null)  showMessageDialog(null, "You didn't enter anything!", "Alert", ERROR_MESSAGE);
    else if (num == MIN_INT)  showMessageDialog(null, "Entry \"" + input + "\" isn't a number!", "Alert", ERROR_MESSAGE);
    else showMessageDialog(null, "Number " + num + " has been registered.", "Info", INFORMATION_MESSAGE);
  • @xristosk I wonder if the Processing version makes a difference. It worked for me with 2.2.1 and 3.0a5. I no longer have 1.5.1.

    Something in the direction of what @Sinchai shared would work: build your own input box that accepts numbers, enter, maybe backspace.

  • I can't run 2.2.1 so I still use 1.5.1. It is a java problem I haven't resolve. But I will test the code on a newer Processing version.

  • edited February 2015
    • @xristosk, if you're using a version that's too old or it's alpha/beta you shoulda been upfront about that! :-w
    • That would help us give you more fitting answers! :-<
    • Nonetheless, even Processing 1.5.1 shouldn't have any problems w/ standard Java API than any other Processing version! 8-X
    • My own JOptionPane example works in my Processing 1.5.1 as flawless as the v2.2.1!
    • Just make sure to remove/rename Processing 1.5.1's "/java/" subfolder.
    • Doing so forces v1.5.1 to use system's own installed Java!
    • Processing v1.5.1 works even w/ latest 64-bit JDK 8!
  • OK, it worked in Processing 2.2.1 Thank you all. I 'm grateful...

Sign In or Register to comment.