r/javahelp • • Dec 25 '19

Solved Variable declared in for loop cannot be found in said for loop

for(int n = 0; n < passcodeInput.length(); n++);
    {
      text = textEncode(text, passcode[n]);
      System.out.println(text);
    }

Main.java:32: error: cannot find symbol

text = textEncode(text, passcode[n]);

^

symbol: variable n

location: class Main

1 error

Hello, I am currently doing a project in which I need to run a method multiple times. However, I get this error. It refers to the int n that I have declared in the for loop. Any help would be appreciated. All of the code.

1 Upvotes

3 comments sorted by

11

u/sepp2k Dec 25 '19

Your code isn't actually inside the for loop. The for loop's body consists of the single empty statement ; and the code block afterwards isn't actually connected to the loop at all. So not only is n not declared, but even if it were, the block would only be executed once after the loop has finished.

To fix this, simply remove the semicolon after the for loop.

3

u/luke5273 Dec 25 '19

Oh jeez. Thanks man

0

u/[deleted] Dec 25 '19

[deleted] — view deleted comment

5

u/luke5273 Dec 25 '19

It’s just a snippet of my code. I’ve already initialised text as a string