public class ForLoop {
        public static void main(String[] args){
            String writtenStuff = 
                    "someone was here!" + 
                    "here i am!"; 
            int numP = 0; 
            int length = yazi.length(); 

            for (int i = 0; i < length; i++){ 
                if (yazi.charAt(i) != 's')


            }//for end 

            System.out.println("the total number of the letters in writtenStuff is " + i +
                    "\n,but the number of letters without b is " + numP);
        }//end method
}//end class 

in the line starting with "system.out..", an error occurs. It says that i cannto be resolved to a variable. I did not understand why i is not considered a value. Is this a scope issue? if so, can you explain the reason behind this error ?

4 Years
Discussion Span
Last Post by GeekTool

Your Problem : Scope of variable "i" is only to loop.
Try to declare a variable before for loop and use it inside for statement.

int i;

Edited by Majestics


Your for loop has a local variable i which is being used in the System.out.println() method. The scope of the variable i only extends within the for loop therefore according to the compiler it is uninitialised.

A simple solution is to declare it in the scope of main()

int i;
for ( i=0; i < length ; i++){

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.