Question

I've got a problem. I written a program, which allows me to count to a specific number and is able to use Prefix and Suffix. I need this in order to build another program. Here's my code (//offen is german for open, //zu for closed)

public class zael{ //offen1
    public static void main(String[] args){//offen2
        int z = 0;
        System.out.println("Bis welche Zahl willst du zaelen?");
        String keins = System.console().readLine();
        int k = Integer.parseInt(keins);
        System.out.println("Willst du einen prefix? (Y/N)");
        String p = System.console().readLine();
        if (p.equals ("Y")){//offen3
            System.out.println("Gib deinen Prefix an!");
            String pref = System.console().readLine();
        }//zu3
        System.out.println("Willst du einen Suffix? (Y/N)");
        String s = System.console().readLine();
        if (s.equals ("Y")) {//offen4
            System.out.println("Gib deinen Suffix an!");
            String suff = System.console().readLine();
        }//zu4
        if (p.equals ("Y")){//offen5
            while(z < k) {//offen6
                if (s.equals ("Y")) {//offen7
                    System.out.println(pref);
                    System.out.print(z+1);
                    System.out.print(suff);
                    z = z + 1;
                }//zu7
                else {//offen8
                    System.out.println(pref);
                    System.out.print(z+1);
                    z = z + 1;
                }//zu8
            }//zu6
        }//zu5
        else {//offen9
            while (z < k){//offen10
                if (s.equals ("Y")) {//offen11
                    System.out.println("Gib deinen Suffix an!");
                    String suff = System.console().readLine();
                    System.out.println(z+1 + suff);
                    z = z + 1;
                }//zu11
                else{//offen12
                    System.out.println(z+1);
                    z = z + 1;
                }//zu12
            }//zu10
        }//zu9
    }//zu2
}//zu1

And the errors I get are:

zael.java:22: error: cannot find symbol
                    System.out.println(pref);
                                       ^
  symbol:   variable pref
  location: class zael
zael.java:24: error: cannot find symbol
                    System.out.print(suff);
                                     ^
  symbol:   variable suff
  location: class zael
zael.java:28: error: cannot find symbol
                    System.out.println(pref);
                                       ^
  symbol:   variable pref
  location: class zael
3 errors
Was it helpful?

Solution 3

This has to do with the scope of your variable:

From Java Language Specification, Section 6.3:

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is visible.

A declaration is said to be in scope at a particular point in a program if and only if the declaration's scope includes that point.

So, analyzing your program...

  1. The String variable named pref is declared in scope #3. Hence it can be accessed by itself and other scopes nested within it.
  2. When you try to access the pref variable in scope #4 in lines 22 and 28.
  3. The same goes for the variable suff.

Now, How To Fix It?

Declare (and initialize) the variables outside the scopes where you need them. For example, you need the pref variable in scope #4. Therefore, declare it in the main() method, i.e. scope #2.

Then modify your if (p.equals ("Y")){... statement as:

if (s.equals ("Y")) {//offen4
    System.out.println("Gib deinen Suffix an!");
    suff = System.console().readLine();
} else {
    // some backup mechanism like System.exit(0);
    // so that it doesn't pose a problem later.

OTHER TIPS

The problem is this line: System.out.println(pref);

You have not defined pref in this scope. The field pref is only defined in main method but cannot be read outside of it and therefore outside your main method the pref field has to be defined as well. (You may define it as static member field as well to reduce code duplication.)

If you indent your code it is easy to see:

    if (p.equals("Y")) {//offen3
        System.out.println("Gib deinen Prefix an!");
        String pref = System.console().readLine();
    }//zu3

In the code block above the variable pref becomes useless as its range ends once the block closes.

    if (s.equals("Y")) {//offen4
        System.out.println("Gib deinen Suffix an!");
        String suff = System.console().readLine();
    }//zu4

This is the same for the variable suff above.

Hence you get errors in the lines below:

System.out.println(pref);
System.out.print(z + 1);
System.out.print(suff);

The easiest way to fix this would be to define both variables outside the if block as follows:

String pref = null;
if (p.equals("Y")) {//offen3
    System.out.println("Gib deinen Prefix an!");
    pref = System.console().readLine();
}//zu3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top