Tile124
Tile124

Reputation: 13

Variable not being defined by if statements

I'm trying to make a program that will make string s1 equal to certain text depending on the hours variable. The problem is when I run the program s1 isn't found. I'm just starting out with Java so I'm not sure if this is really inefficient or if it's something simple I'm missing.

Code:

public class Main {

    public static void main(String[] args) {
        // String Change Test
        int[] arr;
        arr = new int[2];
        arr[0] = 1;
        boolean b1 = arr[0] > 1;
        boolean b2 = arr[0] < 1;
        boolean b4 = 0 > arr[0];
        boolean b3 = b4 && b2;
        boolean b5 = b1 || b3;
        if (b5) {
            String s1 = "You have played for " + arr[0] + " hours!";

        }
        else if (arr[0] == 1) {
            String s1 = "You have played for 1 hour!";

        }
        else if (arr[0] == 5) {
            String s1 = "You have not played at all!";
        }
        else {
            String s1 = "Memory Error in arr[0], Are the hours negative? Is it there?";
        }
        System.out.print (s1);
    }
}

Upvotes: 0

Views: 324

Answers (4)

Batman
Batman

Reputation: 45

Try this..

int[] arr;
arr = new int[2];
arr[0] = 1;
boolean b1 = arr[0] > 1;
boolean b2 = arr[0] < 1;
boolean b4 = 0 > arr[0];
boolean b3 = b4 && b2;
boolean b5 = b1 || b3;
 String s1 = "";
if (b5) {
     s1 = "You have played for " + arr[0] + " hours!";

}
else if (arr[0] == 1) {
     s1 = "You have played for 1 hour!";

}
else if (arr[0] == 5) {
     s1 = "You have not played at all!";
}
else {
     s1 = "Memory Error in arr[0], Are the hours negative? Is it there?";
}
System.out.print (s1);
}

Upvotes: 2

mtyurt
mtyurt

Reputation: 3449

What happens inside a code block, stays in that code block. If you declare a variable in an if block, it's not visible outside of the if block - not even in else if and else cases. Your code shouldn't compile because the last s1 is not declared before.

    String s1;
    if (b5) {
       s1 = "You have played for " + arr[0] + " hours!";

    }
    else if (arr[0] == 1) {
       s1 = "You have played for 1 hour!";

    }
    else if (arr[0] == 5) {
       s1 = "You have not played at all!";
    }
    else {
       s1 = "Memory Error in arr[0], Are the hours negative? Is it there?";
    }
    System.out.print(s1);

This should work properly.

Upvotes: 0

Abdulgood89
Abdulgood89

Reputation: 389

You will need to define String s1 in the beginning of your main method, as such:

     String s1;

Later, when you set s1 (in your if, else statements), you can write:

     s1 = "You have played for......";

That way, s1 will have been declared at the beginning of the code.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

The scope of a variable is the block in which the variable is declared. Blocks start at the opening curly brace and stop at the matching closing curly brace. So you're declaring three different variables that are not visible outside of their block (which is why Java lets you declare it three times with the same name, by the way).

Declare the variable once, outside of the blocks:

String s1;
if (b5) {
    s1 = "You have played for " + arr[0] + " hours!";
}
...

Upvotes: 2

Related Questions