lundi 16 avril 2018

Illegal start of expression inside if statement using objects

I'm having a very frustrating issue at the moment and perhaps the answer lies here?

I want to use an instance of another class(Ending) called changeState the class Ending contains a variable called counter

I want my core class to contain an if statement which closes the entire program if my variable counter reaches 2.

private int counter = 0;
//located in the class Ending

I implemented that using a seperate method addCounter() which goes as

public void addCounter(){
    this.counter ++;
}

I also have a getter which is supposed to return the value of counter

public int getCounter(){
    return counter;
}

however, I can't seem to come up with a fitting if statement which checks if the method getCounter has reached after addCounter();has been invoked several times

My first idea was to use something such as

if(counter.getCounter().equals(2)){
System.exit(0);
}
//I also tried using: 

if(counter.getCounter() == 2)

//however, that didn't work either

both lines give me numerous errors which I can't wrap my head around:

.java:476: error: illegal start of type: if(True.getCounter().equals(2)){

.java:476: error: <identifier> expected: if(True.getCounter().equals(2)){

.java:476: error: ';' expected: if(True.getCounter().equals(2)){

.java:476: error: illegal start of type: if(True.getCounter().equals(2)){

.java:476: error: illegal start of type: if(True.getCounter().equals(2)){

.java:476: error: ';' expected: if(True.getCounter().equals(2)){

Could anyone elaborate on what is going wrong and what should be done to overcome this issue? Thank you in advance!

C.C.

Aucun commentaire:

Enregistrer un commentaire