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 › how to : char array
Page Index Toggle Pages: 1
how to : char array (Read 1423 times)
how to : char array
Nov 27th, 2008, 7:16pm
 
Hi.
I'm having a problom with character array.
What I'm trying to do is to get input from keyboard and store char input into char array and print out the input.
This is what I've done so far.

char [] sentence = new char[100];
//String str1 = new String(sentenc
PFont fontA;
void setup()
{
 size (100,100);
 noStroke();
 background(0);
 fontA = loadFont("BerlinSansFB-Reg-48.vlw");
 textAlign(CENTER);
 textFont(fontA, 10);
}

void draw ()
{
 int i=0, cnt=0;
 while(keyPressed){
   for(i=0; key != '\n'; i++){
     sentence[i] = key;
     //i++;
     cnt++;    
   }
 }
 sentence[i] = '\0';
 printString(cnt);
}

void printString(int cnt)
{
   int x=20, y= 20;
   for(int j=0; j<cnt; j++){
   text(sentence[j], x, y);
   x = x*j;
   y = y*j;
 }
}
Re: how to : char array
Reply #1 - Nov 27th, 2008, 8:14pm
 
It may help to simplify the procedure.  The first problem is that the animation does not complete the loop until the key is released, so you may want to separate the key event to another function.  Try this:


String sentence2;

PFont fontA;
void setup()
{
 sentence2 = "";
 size (100,100);
 noStroke();
 background(0);
 fontA = loadFont("BerlinSansFB-Reg-48.vlw");
 textAlign(CENTER);
 textFont(fontA, 10);
}

void draw ()
{
   
   char[] sentence = sentence2.toCharArray();
   int x=20, y= 20;  
   for(int j=0; j< sentence2.length(); j++){
     text(sentence[j], x*j, y*j);
   }
 
 println("" + x + "  " + y);
}


void keyPressed() {
 sentence2 = sentence2 + key;
}



Does this do what you want?
Re: how to : char array
Reply #2 - Nov 29th, 2008, 2:49am
 
It look much better.
Actually, what I want to do is when the program gets input from keyboard, store the input into array, then display it when I press anything.
For example, when I run program and type "Hello, World!", and press anything, then the program shows "Hello, World!"
I'll keep trying.
Anyway, thanks for your help.
Re: how to : char array
Reply #3 - Nov 29th, 2008, 3:11am
 
I see, so you don't want the character to appear when you hit a letter?

The problem is that the program has no way to distinguish the input between "Typing" and "hitting any key." Either way, keyPressed() event gets called.

Meanwhile, the program needs to keep running, either to display something, or wait for an event to occur.


How would you like the program to know when to display your sentence? Should it be some specified amount of time after the user stops typing, like:


String sentence2;
int countdown;
PFont fontA;
void setup()
{
 countdown = 50;
 sentence2 = "";
 size (500,500);
 noStroke();
 background(0);
 fontA = loadFont("BerlinSansFB-Reg-48.vlw");
 textAlign(CENTER);
 textFont(fontA, 10);
}

void draw ()
{
 background(0);
    if( countdown <= 0){
      countdown = 0;
   char[] sentence = sentence2.toCharArray();
   int x=20, y= 20;  
   for(int j=0; j< sentence2.length(); j++){
text(sentence[j], x*j, y*j);
   }
 
 println("" + x + "  " + y);
    }
   
   
    countdown--;
    println(countdown);
}  


void keyPressed() {
 if (countdown <= 0){
   sentence2 = "";
 }
 countdown = 50;
 sentence2 = sentence2 + key;
}




So, if the user stops typing, it'll display what's typed after a while. How's this?



Also, if you want multiple sentences to be stored, you need an array of String rather than char[].  I recommend using ArrayList rather than array for this purpose.





==UPDATE===
I reread your post and understand what you mean now - you want to wait until the user hits the return key!  so here is the correct code:


String sentence2;
int countdown;
PFont fontA;
void setup()
{
 countdown = 50;
 sentence2 = "";
 size (500,500);
 noStroke();
 background(0);
 fontA = loadFont("BerlinSansFB-Reg-48.vlw");
 textAlign(CENTER);
 textFont(fontA, 10);
}

void draw ()
{
 background(0);
    if( countdown <= 0){
      countdown = 0;
   char[] sentence = sentence2.toCharArray();
   int x=20, y= 20;  
   for(int j=0; j< sentence2.length(); j++){
text(sentence[j], x*j, y*j);
   }
 
 println("" + x + "  " + y);
    }
   
   
    //countdown--;
    println(countdown);
}  


void keyPressed() {
 
 if (key == '\n'){
   countdown = 0;
 }else{
 if (countdown == 0){
   sentence2 = "";
 }
 countdown = 50;
 sentence2 = sentence2 + key;
 }
}


Re: how to : char array
Reply #4 - Nov 29th, 2008, 4:32am
 
Thank you sw01!
This is what I want.  
Now, I'm working with some kind of e-card and it is really helpful.


Re: how to : char array
Reply #5 - Nov 29th, 2008, 6:16am
 
You're welcome.  Here's a parting gift, a take on what you were making:


String sentence2;
int countdown;
PFont fontA;
void setup()
{
 countdown = 50;
 sentence2 = "";
 size (500,500);
 noStroke();
 background(0);
 fontA = loadFont("BerlinSansFB-Reg-48.vlw");
 textAlign(CENTER);
 textFont(fontA, 10);
 smooth();
}

void draw ()
{
 translate(30, 30);
 scale(2);
 background(0);
    if( countdown <= 0){
      countdown = 0;
   char[] sentence = sentence2.toCharArray();
   //int x=20, y= 20;  
int x=200/sentence2.length(), y= 200/sentence2.length();
   for(int j=0; j< sentence2.length(); j++){
     pushMatrix();
     float nx = x*j+frameCount;
     float ny = y*j+ frameCount+100;
     translate(noise(nx/100)*100, noise(ny/100)*100);
     text(sentence[j], x*j, y*j);
     popMatrix();
 }
 println("" + x + "  " + y);
}
   
   
    //countdown--;
    println(countdown);
}  


void keyPressed() {
 
 if (key == '\n'){
   countdown = 0;
 }else{
 if (countdown == 0){
   sentence2 = "";
 }
 countdown = 50;
 sentence2 = sentence2 + key;
 }
}

Page Index Toggle Pages: 1