ylliX - Online Advertising Network
Vertex AI - Antrophic and Mistral models: Why does it require Imegen access?

print the output from a nested for loop after matching each character from a control string with the word from an string arrayh


I have a String array [“reference”, “class”, “method”, “type”, “constructor”, “recursive”] and a String keyWord “structure” and i need to print the words from the array that starts with each character of the keyWord. I removed the duplicates from the keyWord i found with word from the array starts with the charcters from the keyWord but i have trouble with the output.

My output is as follow:
t: type
r: reference
r: recursive
c: class
c: constructor

The desire output is:
t: type
r: reference, recursive
c: class, constructor

Any help is appreciated

Thank you

`

public class Main {
public static void main(String[] args) {

String[] array = {“reference”, “class”, “method”, “type”, “constructor”,”recursive”};
String keyWord = “structure”;

    print(array,keyWord);

}

static void print(String[] array, String keyWord) {

    String result = removeDuplicates(keyWord).toLowerCase();

    for (int i = 0; i < result.length(); i++) {
        for ( int j = 0; j < array.length; j++) {
            if (matchFirstWithLetter(array[j], result.charAt(i))) {
                System.out.print(result.charAt(i) + ": " + array[j] + "\n");


            }
        }
    }
}

static String removeDuplicates(String str) {
    String result = "";
    for (int i = 0; i < str.length(); i++) {
        if (!result.contains(String.valueOf(str.charAt(i)))) {
            result += String.valueOf(str.charAt(i));
        }
    }
    return result;
}

static boolean matchFirstWithLetter(String str, char letter) {
    for (int i = 0; i < str.length(); i++) {
       if (str.charAt(0) == letter) {
           return true;
       }
   }
    return false;
}

}

`



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *