The variable counterexists only within the loop. To refer to it after the loop, you need to define it outside the loop:
import java.io.IOException;
import java.util.Scanner;
public class Chapter_3_Self_Test {
public static void main (String args []) throws IOException {
Scanner sc = new Scanner (System.in);
int counter = 0;
for (char a; a == '.'; counter++) {
a = (char) System.in.read();
}
System.out.println(counter);
}
}
Please note that, on the contrary, it char acan be declared in the scope of the loop for, since it is not used outside the loop.
source
share