0

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);
    }
}
2
  • 4
    You need to declare s1 outside the scope of the conditions. Commented Nov 7, 2016 at 21:09
  • Please declare the variable outside first if statement and use the same variable everywhere. Please read and understand scope of variables. Commented Nov 7, 2016 at 21:10

4 Answers 4

2

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!";
}
...
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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.

2 Comments

Saying String s1 needs to be declared in the beginning of your main method, while it would work, could give the wrong impression.
Yes, true. I should have mentioned something about scope as well. Thank you.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.