java - Syntax error on token ";" ... why? -
why there syntax error on line ( shown below ) ? thanks
import java.util.stringtokenizer; public class tokenizer { public tokenizer() { } int n; string esempio = "ciao dodo sos"; stringtokenizer tok = new stringtokenizer(esempio); // <---- syntax error on token ";" while (tok.hasmoreelements()) system.out.println("" + ++n +": "+tok.nextelement()); }
the compiler attempting associate stringtokenizer
declaration while
loop expecting opening brace {
(for anonymous implementation block) rather semi-colon ;
.
you need use method rather have code in class block:
int n = 0; string esempio = "ciao dodo sos"; stringtokenizer tok = new stringtokenizer(esempio); void dosomething() { while (tok.hasmoreelements()) { system.out.println("" + ++n +": "+tok.nextelement()); } }
a while
statement non-declarative statement must appear in method, static initializer or constructor.
Comments
Post a Comment