java - "...cannot be resolved to a variable." Why not? -
public int getentityindex(string name){ for(int = 0; < entities.length; i++){ if(entities[i].getname().touppercase().equals(name.touppercase())){ break; } } return i; }
this code produces error: i
cannot resolved variable. i'm guessing variables declared inside loop declaration outside scope of remainder of method, unable find information regarding problem specifically.
after analyzing code while, starting see using bad idea (what if entities[i]
never equals name
? method return entities.length - 1
, if match not found. think i'll use while(!found)
approach instead.
to clarify, i'm not asking how fix issue. i'm asking why error occurring.
thanks!
you cannot see i
outside for
loop.
try this:
public int getentityindex(string name) { for(int = 0; < entities.length; i++){ if(entities[i].getname().touppercase().equals(name.touppercase())){ return i; } } return -1; }
ps: use
entities[i].getname().equalsignorecase(name)
instead of
entities[i].getname().touppercase().equals(name.touppercase())
Comments
Post a Comment