Eric Sage
Eric Sage

Reputation: 69

Illegal start of Expression java

I know this is going to be really simple for someone and I can't figure out why the compiler is complaining about this. I have been looking up for some answers and all I can find is a bracket issue but I don't think that is my problem. I am new to Java so any help would be awesome. This is the code that is supposed to be a basic accumulator program.

public class BasicAccumulator implements Accumulator {
  {
    private int digit;
    private int value;

  }

  public int basicAccumulator(int digit, int value)
  {
    digit = 0;
    value = 0;
  }

  public void addDigit(int digit);
  {
    digit = digit + value;
  }

  public void plus();
  {
    value = digit + digit;
  }

  public void minus();
  {
    value = digit - digit;
  }

  public void clear();
  {
    value = 0;
  }

  public int displayValue();
  {
    return value;
  }

}

Upvotes: 2

Views: 11498

Answers (5)

Fritz
Fritz

Reputation: 10055

I'll post my comments directly in your code:

public class BasicAccumulator implements Accumulator {

    //I'd delete this brackets and leave just the private declarations initialized
    //in zero.
    {
        private int digit;
        private int value;    
    }

    //I'm making this an initializing constructor by using the parameters
    //it defines. If you want both digit and value to be set to 0 (or any other value
    //by default) you can make a no argument constructor and invoke it.
    public BasicAccumulator(int digit, int value)
    {
        this.digit = digit;
        this.value = value;
    }

    public void addDigit(int digit); //This semicolon is wrong. Delete it.
    {
        digit = digit + value;
    }

    public void plus(); //This semicolon is wrong. Delete it.
    {
        value = digit + digit;
    }

    public void minus(); //This semicolon is wrong. Delete it.
    {
        value = digit - digit;
    }

    public void clear(); //This semicolon is wrong. Delete it.
    {
        value = 0;
    }

    public int displayValue(); //This semicolon is wrong. Delete it.
    {
        return value;
    }

}

I don't know if this was an example or anything but there are some issues with the logic too, but I'll leave those to you (the minus method in particular, since it will always set the value to 0).

Upvotes: 2

Brian
Brian

Reputation: 17319

Your posted code has three problems.

  1. You have semi-colons after all your methods. The only time you use this is when you have an abstract method (declared abstract in an abstract class or in an interface).
  2. It looks like you tried to create digit and value in an initializer block. Remove the brackets around them.
  3. Constructors do not return a value, so the return type is never included in its signature. You have public int basicAccumulator but it should be just public BasicAccumulator. Also, keep an eye on case-sensitivity.

Here is your fixed code:

public class BasicAccumulator implements Accumulator {

    private int digit;
    private int value;

    public BasicAccumulator(int digit, int value) {
        digit = 0;
        value = 0;
    }

    public void addDigit(int digit) {
        digit = digit + value;
    }

    public void plus() {
        value = digit + digit;
    }

    public void minus() {
        value = digit - digit;
    }

    public void clear() {
        value = 0;
    }

    public int displayValue() {
        return value;
    }
}

Upvotes: 0

Shades88
Shades88

Reputation: 8360

what are those semicolons doing after signatures of functions addDigit, plus, minus, clear and two below that? Remove them. That will do the job

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

public void plus();
public void minus();
public void clear();
public int displayValue();

The above lines in code is the error..

public void plus();
{
    value = digit + digit;
}

Let it be like this...

public void plus()
{
    value = digit + digit;
}

Do this for the remaining methods.....

Upvotes: 0

kosa
kosa

Reputation: 66657

public void plus();

remove semi-colon. It should be:

public void plus()
{ ...
}

Same for displayValue(),minus(), clear(), also. It should be:

Upvotes: 5

Related Questions