We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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; }
Answers
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.
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
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.
Alternative implementation of Character.digit():
@GoToLoop That code doesn't use your num variable, so it's not returning the correct value. Please run example code before posting it.
Oops! Forgot to change ch to num within the
return
as its output! Fixed now! X_XThis shows a dialog asking for a number:
@hamoid Grateful for your post, this is what I want, but i get an error message.
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/
@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.
Online textbox input: O:-)
http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest
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.
OK, it worked in Processing 2.2.1 Thank you all. I 'm grateful...