package ClassTest3;
public class IdentifyPosiibleWords {
public static String identifyWords(String input1, String input2) {
int i, j;
input1 = input1.toUpperCase();
input2 = input2.toUpperCase();
String word = "", res = "";
String[] words = input2.split(":");
System.out.println(input1.length());
for (i = 0; i < words.length; i++) {
word = words[i];
if (input1.length() == word.length()) {
for (j = 0; j < input1.length(); j++) {
if (input1.charAt(j) != '_' && input1.charAt(j) != word.charAt(j)) {
break;
}
}
if (j == input1.length()) {
res = res + word + ":";
}
}
}
return res.length() == 0 ? "ERROR-009" : res.substring(0, res.length() - 1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String input1 = "Fi_er";
String input2 = "Fever:filer:Filter: Fixer:fiber:fibre:tailor:offer";
String output = identifyWords(input1, input2);
System.out.println(output);
}
}
This program has no error, the only this i uploaded here i have a doubt in this code
see the line 22 : if (j == input1.length()) this inner nested loop update the j in line 7 but at the end of the matching word found, j will be 4 and how it will pass the input1.length that is 5.
someone please explain it to me.
>Solution :
After the loop exited, j < input1.length() is false.
However, it has just been incremented by one.
Therefore, at this point, assuming you entered the loop body at least once:
j == input1.length() is true.