String not declaring in conditional expression?

This is the second script I've ever written in Java since I just started writing a book, 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 expression in a loop. Unfortunately, it will not compile because the line is supposedly undeclared. Then, when I pulled it out of the conditional, but still in the loop, it worked fine. Did I miss something?

WORKERS:

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; } 

DOES NOT WORK

  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) 
+4
source share
6 answers

If you have a string defined in an if statement block, it will not be in scope.

+6
source

The problem is related to the scope of variables. In a non-working example, you declare strings inside the if and else scope. So, when you try to print the values, they go beyond the bounds.

 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!"); 

A working example works because when a line is declared at the top of the loop, it has scope in all of this.

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

This behavior may seem unintuitive, 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 } 
+5
source

This is due to the definition of the scope.

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

But when you declare it inside if-statement blocks, they are only visible inside. This is why you later get undeclared variables.

An easy way to express this - this declaration is visible only to the inner majority of brackets {} in which it is declared.

+3
source

Braces determine the scope. In the second version, the variable sOrNot is undefined, because it is defined only in the scope of the if .

 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 the area

+3
source

This is what Java does to avoid error. The idea is that you assign the value of sOrNot to only one of the conditional statements, but not to the other statement, the program can run its course and never assign the value of sOrNot, but it will still try to print. If this happens, the program will fail.

As other people have said, these are problems with reach, but as I have just defined, it is easier for new programmers to understand.

Hope this helps!

+2
source

The string sOrNot declared inside the if statement refers only to the IF statement and cannot be used outside of it.

0
source

All Articles