Hi,
This is an exam question from a past paper i'm going through. The question asks what is the intended purpose of the loop and what's wrong with the code.
I said the purpose of the loop is to find the first occurrence of a lowercase character. I am not clear what the problem with the loop is. I'm pretty sure it's to do with the boolean operator (!found) included in the for loop statement. But it seems to make sense to have it there so the loop doesnt continue when the first lowercase char is found. Should i use break to jump out of the loop instead ?
public class sampleQuestion {
public static void main(String[] args){
String text = "HElLo";
boolean found = false;
for(int i=0;!found && i<text.length();i++)
{
char ch = text.charAt(i);
if(Character.isLowerCase(ch))
{
found = true;
System.out.println(ch);
}
}
}
}
Thanks for any tips