Kevassi
Kevassi

Reputation: 247

Why is class, interface, or enum expected?

How and why am I getting the compiler error ("class, interface, or enum expected")? Here is the code:

public class RolloverCounter{
    private int counter=0;
    private int max;
    public RolloverCounter(int a){
      if(a<0){
        max = a;
      }
    }
    public void increment()
    {
       for(int i=0;i<=max;i++)
       {
         counter++;
         System.out.println(counter);
         if(counter==max){
           counter=0;
         }
       }
    }
    public void decrement(){
        for(int i=0;i<=max;i++)
          counter--;
        if(counter<0)
        {
          counter=max;
        }
        System.out.println(counter);
   }
   public String toString() {
      return counter;
   }
   public void reset(){
      counter = 0;
   }
}
}

What have I done wrong?

Upvotes: 0

Views: 269

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

Your toString() method isn't returning a String,

public String toString() {
  return counter;
}

should be something like

public String toString() {
    return String.valueOf(counter);
}

Finally, you appear to have an extra closing brace (at the end) in your code as posted.

Upvotes: 4

Related Questions