Search code examples
javastringcounter

Java cannot find symbol during compiling


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

Solution

  • 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.