user3794577
user3794577

Reputation: 55

Java could not resolve to a variable

if(text.contains(authcode) && text.contains("balance")){
    String balUser = text.split("]")[0];
    event.getSession().send(new ClientChatPacket("/money"));
}
if(text.contains("Balance: $")){
    text = text.split(": ")[1];
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    event.getSession().send(new ClientChatPacket("/m " + balUser + text));
}

Unfortunately balUser (In the second IF statement) is highlighted in eclipse as 'Cannot be resolved to a variable'. I was just wondering if I have done some incorrect syntax somewhere.

Upvotes: 0

Views: 114

Answers (5)

AlexR
AlexR

Reputation: 115328

Examine your code structure:

if() {
    String balUser = ...;
} else {
    event.getSession().send( ...   balUser);
}

I hope that now it is obvious that the variable is declared in fist block and is referenced in second block where it does not exist. Generally scope of all identifiers in java and all c-like languages is limited by a pair of surrounding { and }. The inner scope however can see identifiers from the outer scope, so this code is correct:

int i = 5;
{
   int j = i;
}

Upvotes: 0

forgivenson
forgivenson

Reputation: 4435

You declared the variable balUser inside the first if statement, so its scope is that first if statement. You should declare it outside of the if statement if you want to use it elsewhere.

String balUser = null;
if(text.contains(authcode) && text.contains("balance")){
    balUser = text.split("]")[0];
    event.getSession().send(new ClientChatPacket("/money"));
}
if(text.contains("Balance: $")){
    text = text.split(": ")[1];
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    event.getSession().send(new ClientChatPacket("/m " + balUser + text));
}

Upvotes: 0

AntonH
AntonH

Reputation: 6437

Your variable String balUser is declared inside a pair of curly brackets, so it's scope is limited to that block of code.

if you want it to be known elsewhere, you need to declare it where it can be seen by both blocks.

In your case:

String balUser = null; // variable declared here
if(text.contains(authcode) && text.contains("balance")){
    balUser = text.split("]")[0]; // remove declaration, just assignation
    event.getSession().send(new ClientChatPacket("/money"));
}
    if(text.contains("Balance: $")){
        text = text.split(": ")[1];
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    event.getSession().send(new ClientChatPacket("/m " + balUser + text));
}

Upvotes: 0

Mena
Mena

Reputation: 48404

You are declaring your balUser String within the first if statement, hence it is scoped locally.

Declare it outside the first if statement and check for nulls in the second to make this go away.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

Yes. The balUser is defined in the scope of the if. Just define it outside the if statement:

String balUser = null;
if(text.contains(authcode) && text.contains("balance")) {
    balUser = ....
}

Upvotes: 1

Related Questions