We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › String and Numerical Input and Handling
Page Index Toggle Pages: 1
String and Numerical Input and Handling (Read 1416 times)
String and Numerical Input and Handling
Oct 10th, 2008, 10:31am
 
Friends:

How do I prompt for text and numerical input?  In BASIC it would be
input "What is the length? ";length 'for a numerical input

and for a string:
input "What is your name? ";name$ 'for a string

How do I do these things as well as manipulate strings in Processing?  

Remarkably, I bought the giant Processing book and string I/O isn't in there?

Thanks for your help,

Migs
Re: String and Numerical Input and Handling
Reply #1 - Oct 10th, 2008, 12:17pm
 
Hi,

Processing uses Java Strings, and as such there would be much more info on such things in just about any Java book. I suggest the Sun's Java tutorial on Strings as a good place to start. Processing books are more likely to focus on the graphics and interactivity aspects of Processing.
For input, you have two choices, roll your own code which accepts key presses and adds them to a string buffer until enter is pressed, or add some text input components. To do the later you will need a basic understanding of an event model and then look at the libraries page for Graphical Interface components.

Regards,

Stephen
Re: String and Numerical Input and Handling
Reply #2 - Oct 10th, 2008, 3:10pm
 
Thanks Stephen.  Looks scary.  I'll check the Sun java tutorial first and see if that is not an unsurmountable obstacle.

I come from the Pascal, Basic, Fortran days with a little C++ thrown in the mix.

Thanks for your help!

Migs
Re: String and Numerical Input and Handling
Reply #3 - Oct 10th, 2008, 9:26pm
 
Since Processing is all visual, we don't have any support for typing on the console the way that you do in BASIC or a similar language. In Java, you use System.in.readLine() to implement the INPUT command, but that's not available in Processing.

The best thing to do is check out something like the controlp5 library, which will help you get started with a text entry field (I think it has one), or if not, use the keyPressed() method and keep track of keys as they're typed.
Re: String and Numerical Input and Handling
Reply #4 - Oct 10th, 2008, 10:23pm
 
Hi fry!

I think a high performance language should also have I/O of the lowest level kind - to humans via the keyboard.  This wonderful language is built for artists.  I cannot conceive an artist coding a simple text input or string parsing just to interact with a program.
Maybe I'm too old, but in my day nothing would have been possible were we not able to input strings and numbers (via keyboard) into programs to control the flow or to achieve output.  I sure hope someone writes a string and keyboard library!

All the very best,

Migs
Re: String and Numerical Input and Handling
Reply #5 - Oct 10th, 2008, 11:18pm
 
Hi Migs,

I don't mean to answer for Fry, and of course I could not if I wanted to. I suspect you are seeing the usefulness (and possibly the general purpose) of Processing differently than its creators. General key and mouse input is extremely easy in Processing, there are built-in methods to handle it. You have a common but special case, the input of an entire line of text, and this is also easy, if you will allow me to demonstrate. The creators cannot (and should not in my opinion) attempt to foresee and implement every possible interaction the user might want, instead they give the building blocks with which to develop your own specialised interactions. The code below assumes you are familiar with the event model of programming. Bewarned, it does not use StringBuffer and it does not handle every key and there is only special processing for backspace and enter. Feel free to ask if you have any questions.

Regards,

Stephen

String lastInput = new String();
String currentInput = new String();

void setup()
{
 size(800, 600);
 smooth();
 PFont font = loadFont("Arial-Black-48.vlw");
 textFont(font);
 textAlign(CENTER);
}

void draw()
{
 background(255, 255, 255);
 fill(0);
 text(lastInput, width/2, height/2);
 fill(255, 0, 0);
 text(currentInput, width/2, height*.75);
}

void keyPressed()
{
 if(key == ENTER)
 {
   lastInput = currentInput = currentInput + key;
   currentInput = "";
 }
 else if(key == BACKSPACE && currentInput.length() > 0)
 {
   currentInput = currentInput.substring(0, currentInput.length() - 1);
 }
 else
 {
   currentInput = currentInput + key;
 }
}
Re: String and Numerical Input and Handling
Reply #6 - Jun 25th, 2009, 10:20am
 
Hi, I Have just made little correction so it works also with Processing 1.x
I hope it helps.
Ciao

String lastInput = new String();
String currentInput = new String();
PFont myFont;
void setup()
{
size(800, 600);
smooth();
  myFont = createFont("FFScala", 32);
textFont(myFont);
textAlign(CENTER);
}

void draw()
{
background(255, 255, 255);
fill(0);
text(lastInput, width/2, height/2);
fill(255, 0, 0);
text(currentInput, width/2, height*.75);
}

void keyPressed()
{
if(key == ENTER)
{
  lastInput = currentInput = currentInput + key;
  currentInput = "";
}
else if(key == BACKSPACE && currentInput.length() > 0)
{
  currentInput = currentInput.substring(0, currentInput.length() - 1);
}
else
{
  currentInput = currentInput + key;
}
}
Re: String and Numerical Input and Handling
Reply #7 - Jun 26th, 2009, 12:57am
 
I'd suggest one other condition before the final else:

else if(key == CODED) {
 // ignore...
}

That will take care of SHIFT.  You might also want to consider the following from keyCode:

"If you're making cross-platform projects, note that the ENTER key is commonly used on PCs and Unix and the RETURN key is used instead on Macintosh. Check for both ENTER and RETURN to make sure your program will work for all platforms."
Page Index Toggle Pages: 1