Warkgnall
Warkgnall

Reputation: 33

Object not recognized outside if-else clauses

In the if-else branches I declared two different PrintStream objects with the same name. However, when I use the PrintStream object later the compiler says that it "cannot find symbol". Why doesn't it see the object I created? The object works withing the if-else branch it was declared in. Here's the code:

System.out.println("Welcome. Would you like to continue a previous adventure, or begin anew?\n(type: new or old)");
status = scan.nextLine();

if(status.equals("new")) {
    System.out.println("What do you call yourself?");
    name = scan.nextLine();

    System.out.println("And what shall be the title of your saga, after you are gone?");
    game = scan.nextLine();

    System.out.println("Very well. Prepare yourself, for this is the beginning of your end...\n...\n...\nAre you ready?");
    status = scan.nextLine();

    System.out.println("Not that it matters. Let us begin.");
    status = scan.nextLine();

    File file = new File(game + ".txt");
    PrintStream print = new PrintStream(file);
    print.println(name);
    old = false;
} else {
    System.out.println("So you're still alive? What was the title of your tale?");
    game = scan.nextLine();

    File file = new File(game + ".txt");
    Scanner gscan = new Scanner(file);
    String save = "";

    while (gscan.hasNextLine()) {
        save += gscan.nextLine();
        save += "\n";
    }

    System.out.println(save);
    PrintStream print = new PrintStream(file);
    print.println(save);

    old = true;
    name = scan.nextLine();
}

if(old) {

}

print.println("beans");

Upvotes: 3

Views: 2127

Answers (5)

fastcodejava
fastcodejava

Reputation: 41097

Why define PrintStream print twice? You need to define it only once in this case outside the if.

Upvotes: 0

eliot
eliot

Reputation: 1329

You are experiencing a scoping problem. You need to declare it outside the if-else statement if you want to use it outside the if-else statement.

There are different levels of scoping in Java programs. A scope is generally defined by a set of curly braces. However, there is also public, private, and protected types that allow for use of more global variables than just inside their braces.

Each of these scopes can have their own variables that are not available elsewhere.

Upvotes: 3

Jedi Knight
Jedi Knight

Reputation: 581

What happens in a scope stays in the scope.

if(some condition)
{
  int x = 10;

}

System.out.println(x);

This will not work because the scope of x is limited only till the if block. If you want it to live outside the if block, then declare it outside the if block.

Upvotes: 0

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

Take a look at these two pieces of code:

String s;
if ( foo ) {
 s = "one";
} else {
 s = "two";
}


if ( foo ) {
 String s = "one";
} else {
 String s = "two";
}

In the first one, s is available after the if/else. In the second, s is only available within the {} (if/else blocks). Each block happens to have a variable with the same name, but it isn't the same variable. And it isn't available later.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

You need to declare the variable outside the if statement, and then assign it in the two branches, like this:

PrintStream print;
if (status.equals("new")) {
    ...
    print = new PrintStream(file);
} else {
    ...
    print = new PrintStream(file);
}

Better yet, you can set the file inside the if, and then create PrintStream after the conditional:

if (status.equals("new")) {
    ...
} else {
    ...
}
File file = new File(game + ".txt");
PrintStream print = new PrintStream(file);

Upvotes: 3

Related Questions