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 ?

Recommended Answers

All 3 Replies

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;

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++){


Thank you guys :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.