Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java conditional Statement Understanding

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading