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 › Problems with Class Char
Page Index Toggle Pages: 1
Problems with Class Char (Read 1437 times)
Problems with Class Char
May 20th, 2010, 9:30am
 
This is a small program that intend to print "p" between every two vowels. ae, oi, eu, ect.

import java.lang.*;

void setup(){
 String word = "aeiou";
 char le_1, le_2;

 for(int i=1;i<word.length();i++){
   le_1 = toLowerCase(word.charAt(i-1));
   le_2 = toLowerCase(word.charAt(i));
   print(le_1);

   if( le_1.equals("a") &&
       le_1.equals("e") &&
       le_1.equals("i") &&
       le_1.equals("o") &&
       le_1.equals("u") ){
       
       if( le_2.equals("a") &&
           le_2.equals("e") &&
           le_2.equals("i") &&
           le_2.equals("o") &&
           le_2.equals("u") ){
       
           print("p");
       }
   }
 }
}

After running this, I got message that the function toLowerCase (char) doesn't exist.
Re: Problems with Class Char
Reply #1 - May 20th, 2010, 9:39am
 
another problem, you have to change "a" to 'a' to check if they are equal, you cant compare a String "a" with a char 'a'
Re: Problems with Class Char
Reply #2 - May 20th, 2010, 9:41am
 
for the lowercase Problem, you have to write

Character.toLowerCase()
Re: Problems with Class Char
Reply #3 - May 20th, 2010, 6:27pm
 
Thanks a lot Cedric. It worked. But after, I had to declare "le_1" and "le_2" as "Character" and not "char". Can you tell me the difference?

Do you use other website to support you in processing?

Thanks again. Nice blog, I will dig on it later.
Re: Problems with Class Char
Reply #4 - May 22nd, 2010, 12:47am
 
You have to choose between char and String, they are different in Java/Processing.
With String:
Code:
void setup(){
String word = "aeiou";
String le_1, le_2;

for(int i=1;i<word.length();i++){
le_1 = word.toLowerCase().substring(i-1, i);
le_2 = word.toLowerCase().substring(i, i+1);
print(le_1);

if( le_1.equals("a") ||
le_1.equals("e") ||
le_1.equals("i") ||
le_1.equals("o") ||
le_1.equals("u") ){

if( le_2.equals("a") ||
le_2.equals("e") ||
le_2.equals("i") ||
le_2.equals("o") ||
le_2.equals("u") ){

print("p");
}
}
}
}

Note the change from && to ||, a string can't be equals to several different strings...
With char:
Code:
void setup(){
String word = "aeiou";
char le_1, le_2;

for(int i=1;i<word.length();i++){
le_1 = word.toLowerCase().charAt(i-1);
le_2 = word.toLowerCase().charAt(i);
print(le_1);

if ( ( le_1 == 'a' ||
le_1 == 'e' ||
le_1 == 'i' ||
le_1 == 'o' ||
le_1 == 'u' ) &&
( le_2 == 'a' ||
le_2 == 'e' ||
le_2 == 'i' ||
le_2 == 'o' ||
le_2 == 'u' ) )

print("p");
}
}

with a small variant on the logic... Equality test is simpler, too!

I should make a lowercase copy of the string instead of transforming it two times per loop, but here, it isn't important...
Re: Problems with Class Char
Reply #5 - May 22nd, 2010, 11:46am
 
I think PhiLo covered it all pretty nicely.

For cases like this that become a little more complicated, for instance that instead of for 5 vowels, want to check for a white/blacklist of letters/words, I'd use something like this:

Code:
boolean valueExistsInArray(String[] arr, String value) {

 for(int i = 0; i < arr.length; i++) {
   if(arr[i].equals(value)) {
return true;
   }
 }

 return false;
}


But I like the result of your little code very much Smiley
Re: Problems with Class Char
Reply #6 - May 22nd, 2010, 12:16pm
 
Code:
char[] vowels = {'a', 'e', 'i', 'o', 'u'};

String string = "I should make a lowercase copy of the string instead of transforming it two times per loop, but here, it isn't important... ";
String[] words = splitTokens(string.toLowerCase(), " ,.?!");

String[] wopords = new String[0];

void setup() {
for(int i = 0; i < words.length; i++) {
String wopord = "";
for(int j = 0; j < words[i].length(); j++) {
if(valueExistsInArray(vowels, words[i].charAt(j))) {
wopord += "op";
}
wopord += words[i].charAt(j);
}
wopords = append(wopords, wopord);
}

println(join(wopords, " "));
}

boolean valueExistsInArray(char[] arr, char value) {

for(int i = 0; i < arr.length; i++) {
if(arr[i] == value) {
return true;
}
}

return false;
}


Lips Sealed
Page Index Toggle Pages: 1