Mr. Lavalamp
Mr. Lavalamp

Reputation: 1888

String not declaring in conditional?

This is the second script I have ever written in Java, as I have just started a book on it, so I apologize for the inevitably ridiculous question.

I am writing a script to sing "99 bottles of beer on the wall" and tried to declare a new String variable in a conditional within a loop. Unfortunately, it would not compile because the string was supposedly undeclared. Then, when I pulled it OUT of the conditional, but still in the loop, it worked fine. Am I missing something here?

WORKING:

    while (beersLeft >= 0) {
        String sOrNot = "";
        if (beersLeft > 1) {
            sOrNot = "s";
        } else {
            sOrNot = "";
        }
        System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!");
        System.out.println("Take one down, pass it around, "+beersLeft+" bottle"+sOrNot+" of beer on the wall!");
        System.out.println();

        beersLeft = beersLeft-1;
    }

NOT WORKING

    while (beersLeft >= 0) {
        if (beersLeft > 1) {
            String sOrNot = "s";
        } else {
            String sOrNot = "";
        }
        System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!");
        System.out.println("Take one down, pass it around, "+beersLeft+" bottle"+sOrNot+" of beer on the wall!");
        System.out.println();

        beersLeft = beersLeft-1;
    }

ERROR:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at ch01.BottlesOfBeer.main(BottlesOfBeer.java:21)

Upvotes: 4

Views: 282

Answers (6)

Reimeus
Reimeus

Reputation: 159754

Braces define scope. In the second version the variable sOrNot is undefined as it is only defined in the scope of the if statement.

while (beersLeft >= 0) {
    if (beersLeft > 1) {
        String sOrNot = "s";
    } else {
        String sOrNot = "";
    }

    // sOrNot out of scope here - compilation error!
    System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!");
    ...
 }

In the first verson sOrNot is in scope

Upvotes: 3

GM-Xile GM-Xile
GM-Xile GM-Xile

Reputation: 321

The String sOrNot declared inside the if statement only belongs to the IF statement and cannot be used outside it.

Upvotes: 0

Kyurem
Kyurem

Reputation: 1869

This has to do with scoping.

When you declare it outside the if-statements, the declaration is visible for the entire loop.

But when you declare it inside the if-statement blocks, they are only visible from within. That's why you get undeclared variable errors later on.

An easy way to put this is that declaration is only visible to the inner most set of brackets {} that it is declared in.

Upvotes: 3

user1599559
user1599559

Reputation:

The issue relates to variable scope. In the not working example, you are declaring the strings inside the scope of the if and else. So, then when you try to print the values, they are out of scope.

if (beersLeft > 1) {
    String sOrNot = "s";
    // sOrNot scope ends
} else {
    String sOrNot = "";
    // sOrNot scope ends
}
// sOrNot does not exist in this scope.
System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!");

The working example works because when the string is declared at the top of the loop, it has scope throughout the whole thing.

while (beersLeft >= 0) {
   // has scope for rest of loop
   String sOrNot = "";
      ....
}

This behavior might seem nonintuitive, but it allows you to do things like this:

for (int i = 0; i < 10; i++) {
  // do something
}
// The previous i is out of scope, so creates a new one
// without having the names "clash"
for (int i = 5; i >= 0; i--) {
  // do something else
}

Upvotes: 5

Cerberus136
Cerberus136

Reputation: 101

This is something that Java does to avoid an error. The idea is if you only assign a value to sOrNot in one of the conditional statements, but not the other statement, the program can run it's course and never assigned sOrNot a value, but it will still try to print. If this happens the program will error out.

As other people have said, it's scope issues, but in the way I just defined it seems to be easier for new programmers to understand.

Hope this helps!

Upvotes: 2

Andrew
Andrew

Reputation: 1784

If you have the String defined in the if statement block it will not be in scope.

Upvotes: 5

Related Questions