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 & HelpPrograms › How to clear a char[] array
Page Index Toggle Pages: 1
How to clear a char[] array (Read 2095 times)
How to clear a char[] array
Jun 25th, 2009, 8:54am
 
Hi,
im a beginner. I'd like to input a string of characters via keybaord and echo it on screen (like scanf() in C). I dont find any function in processing comparable to scanf. So I tried

   String strng = new String(chr);  

where
   char[] chr = new char[80];

It works only once, since the string strng is not empty anymore. I tried to clear it by

   for (i = 0; i < 80; i++)
   chr[i++] = blank;

The result is not satisfying because only every second symbol equals blank.

What should I do instead?
Undecided
Re: How to clear a char[] array
Reply #1 - Jun 25th, 2009, 9:38am
 
this

for (i = 0; i < 80; i++)
  chr[i++] = blank;

only clears the alternate characters because you're incrementing i twice in each loop, once in the chr[i++] and again in the for(). chr[i] would work.

but you can always

chr = new char[80];

whenever you need to blank the array. leave the initial char[] out because you're not defining the array again - you can only do that once.
Re: How to clear a char[] array
Reply #2 - Jun 25th, 2009, 9:45am
 
Not sure I follow why you create a new string...  To add the key value to the array you just need to reference the position in the array:

chr[counter] = key;

...where 'counter' is a variable you add to at each key press and when it reaches the limit of the array you reset it 0.  To iterate through and 'empty' the array you're on the right track:

for (i = 0; i < chr.length; i++) {
  chr[i] = "";
}

but I see someone's suggested a quicker option.

chr.length just returns the length of the array - if you decide to change it at some point you won't need to update this value Wink

Having said that it might also be worth looking at ArrayList
Page Index Toggle Pages: 1