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…
- The
String
variable namedpref
is declared in scope #3. Hence it can be accessed by itself and other scopes nested within it. - When you try to access the
pref
variable in scope #4 in lines 22 and 28. - 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.
solved Java cannot find symbol during compiling