Sergeant ROFLcopter
Sergeant ROFLcopter

Reputation: 9

What is wrong with this that would return the following error?

package data_structures;

import java.util.StringTokenizer;

public class ExpressionEvaluator implements Stack, Queue {
    private String userInput;
    public ExpressionEvaluator() {
        Userinput = null;
    }

    Iterator<E> st = new StringTokenizer();
    token = st.next();
    while (st.hasMoreTokens()) {

The problem has already occured at this point so I'm not continuing the code

The errors:

data_structures/ExpressionEvaluator.java:10: illegal start of typejava
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: <identifier> expected
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: ';' expected
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: illegal start of type
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: <identifier> expected
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: ';' expected
while (st.hasMoreTokens()) {

Upvotes: 0

Views: 138

Answers (3)

Yogendra Singh
Yogendra Singh

Reputation: 34367

Update you constructor as below because variable naming is not right:

  public ExpressionEvaluator() {
      userInput = null;
  }

Move the code below(UPDATED) the constructor code in some method as it can't exist standalone e.g.

public void evaluate(){
   StringTokenizer st = new StringTokenizer(userInput);
   while (st.hasMoreTokens()) {
      String token = st.nextToken();
      //manage your processing logic here
   }
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

This section

Iterator<E> st = new StringTokenizer();
token = st.next();
while (st.hasMoreTokens()) {

Is not with a method or static initaliser. All code must appear within a method inside a class

Upvotes: 1

kosa
kosa

Reputation: 66637

Following code should be inside a method. Not directly inside a class.

Iterator<E> st = new StringTokenizer();
    token = st.next();
    while (st.hasMoreTokens()) {

Upvotes: 2

Related Questions