Olá! estou estudando Java, mais precisamente esse código abaixo:
public class TestIsSubString {
private static boolean isSubString(String item, String line) {
int i, j;
boolean encontrado = false;
for ( i = 0; (i < line.length()) && !encontrado; i++) {
j = 0;
encontrado = false;
while (!encontrado ) {
if (j >= item.length()) {
break;
} else if ((i+j) >= line.length()) {
break;
} else if (item.charAt(j) != line.charAt(i+j)) {
break;
} else {
j++;
if (j == item.length()) {
encontrado = true;
}
}
}
}
return encontrado;
}
public static void main(String[] args) {
String text = "The cat in the hat.";
System.out.println("isSubString(\"cat\", \"The cat in the hat.\") "
+ isSubString("cat", text));
System.out.println("isSubString(\"bat\", \"The cat in the hat.\") "
+ isSubString("bat", text));
System.out.println("isSubString(\"The\", \"The cat in the hat.\") "
+ isSubString("The", text));
System.out.println("isSubString(\"hat.\", \"The cat in the hat.\") "
+ isSubString("hat.", text));
}
}
Não entendi linha [color=red]12[/color]
else if ((i+j) >= line.length()) {
ou seja, porque (i+j) maior ou igual ao tamanho da linha, ele esta somando o tamanho da palavra com o tamanho da linha… sinceramente não consegui entender a lógica do negócio…