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

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -