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 › It gets an Expecting DOT error
Page Index Toggle Pages: 1
It gets an Expecting DOT error (Read 5729 times)
It gets an Expecting DOT error
Dec 25th, 2009, 11:57am
 
char[] str2char (String string) {
 char[] chara;
 for (i=0; i< string.length(); i++)
 {
   chara[i] = string.charAt(i);
 }
 return chara;
}

I just had a bit of a run-around trying to work out why this function intialisation wouldn't compile.
If I use String[] instead, It will cause a "maybe missing a semicolon"
crazy~~ Cheesy
Re: It gets an Expecting DOT error
Reply #1 - Dec 25th, 2009, 12:48pm
 
The problem lies in the code BEFORE the fragment you just posted.
Re: It gets an Expecting DOT error
Reply #2 - Dec 25th, 2009, 1:34pm
 
Thanks, JR
but I copied only these lines into a new sketch and ran it.
What makes me confusing is using a String[],  the message is different!

The function is really simple, It converts a string into a character array.
Why choose char[] is because I can use a "new String(chara)" to merge them back into a string after operation.  simple and clean. Roll Eyes
Re: It gets an Expecting DOT error
Reply #3 - Dec 25th, 2009, 1:44pm
 
You would better use toCharArray() which is a built-in Java method.

Code:
String s = "mystring";
println(s.toCharArray());


Full reference for String object is here :
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
Re: It gets an Expecting DOT error
Reply #4 - Dec 25th, 2009, 1:48pm
 
Btw, about the DOT error : it's because you have no setup() method in your sketch. Add void setup() {} somewhere after your method, and it will work (there are other errors in your code, though, that you will be warned of).
Re: It gets an Expecting DOT error
Reply #5 - Dec 25th, 2009, 11:30pm
 
OO wrote on Dec 25th, 2009, 1:34pm:
The function is really simple, It converts a string into a character array.
Why choose char[] is because I can use a "new String(chara)" to merge them back into a string after operation.  simple and clean. Roll Eyes


...and probably redundant. What is it exactly that you are trying to do
After which operation do you want to convert them back to a string.
Re: It gets an Expecting DOT error
Reply #6 - Dec 26th, 2009, 5:09am
 
Thanks for antiplastik's method, it works. but there are still some syntax error exist.  

Code:
void setup() {
 size(200,200);
String stringA = "   aaaa    aaa aaa";
String stringB = "xxxx xxxx   xxxxxxxxxxx    xxx";
replace(stringA,stringB);

}

void  replace(String stringA,String stringB) {
 if ((stringA != null) || (stringB != null)) {
   String outString;
   char[] charA = stringA.toCharArray();
   char[] charB = stringB.toCharArray();
   int maxium = max (charA.length,charB.length);    
   charA = expand(charA,maxium);
   charB = expand(charB,maxium);
   for (int i=0; i < maxium ; i++) {
if (charB[i] !=' ') {
 charA[i] = charB[i];
}
   }  
   outString = new String(charA);
   println(charA);
   println(outString);
 //  return outString;
 }
}


this can be complied, and works well.
but what expecting is return the String back from this function.

Code:
 void setup() {
 size(200,200);
String stringA = "   aaaa    aaa aaa";
String stringB = "xxxx xxxx   xxxxxxxxxxx    xxx";
String x = replace(stringA,stringB);
Println (x);

}

String  replace(String stringA,String stringB) {
 if ((stringA != null) || (stringB != null)) {
   String outString;
   char[] charA = stringA.toCharArray();
   char[] charB = stringB.toCharArray();
   int maxium = max (charA.length,charB.length);    
   charA = expand(charA,maxium);
   charB = expand(charB,maxium);
   for (int i=0; i < maxium ; i++) {
if (charB[i] !=' ') {
 charA[i] = charB[i];
}
   }  
   outString = new String(charA);
   //println(charA);
   // println(outString);
   return outString;
 }
}

I get a " This method must return a result of type String"   Cheesy

This coding is a part of practice of string operation.
what I want is using these codes animating some ascii images.  the characters on the static background can be replaced by the dynamic ones if overlapping happen.  Cool


Re: It gets an Expecting DOT error
Reply #7 - Dec 26th, 2009, 5:26am
 
What if stringA or stringB is null? What does your replace() method return, then?

Your method should return a string (or null), whatever occurs inside.
Re: It gets an Expecting DOT error
Reply #8 - Dec 26th, 2009, 6:40am
 
In other words, add a return null (or return "") line just before the end of the method.
Re: It gets an Expecting DOT error
Reply #9 - Dec 28th, 2009, 9:14am
 
Don't worry, the original data is a string array, so when the string[i] is null, just leaving it null is OK. my problem is I can println the data in string, but I cannot return it in string. Huh Huh  How strange!
Re: It gets an Expecting DOT error
Reply #10 - Dec 28th, 2009, 9:16am
 
Maybe I could use a Matrix to deal with all characters in one time.  but I were faced this problem some day. Huh
Re: It gets an Expecting DOT error
Reply #11 - Dec 28th, 2009, 9:37am
 
Quote:
Don't worry, the original data is a string array, so when the string[i] is null, just leaving it null is OK.


If you want to solve your "this method must return a result of type string" problem, you need to put a return statement outside of the if {} enclosure. Just add return null as below :

Quote:
String replace(String stringA,String stringB) {
 if ((stringA != null) || (stringB != null)) {
   ...
   return outString;
 }
 return null;
}


Page Index Toggle Pages: 1