I have 2 Arrays: one contains words (words_array
) and the other contains letters (letters_array
)
I loop through the words_array
and inside the loop I loop through the letters array
and in the second loop I make a condition "if the word contain a letter in the letters array divide it by some way if else it should print the word as it is "
but it doesn't work because it prints duplicated words.
Here is my code:
for (int i = 0; i < words_array.length; i++) {
for (int j = 0; j < letters_array.length ;j++ ) {
if (arryWords[i].contains(letters[j]+"")) {
int wordLength = arryWords[i].length();
int letterIndex = arryWords[i].indexOf(letters[j]);
String sub = arryWords[i].substring(0, letterIndex + 1);
System.out.println(sub);
System.out.println(arryWords[i].substring(letterIndex + 1, wordLength));
} else if (!arryWords[i].contains(letters[j] + "")) {
System.out.println(arryWords[j]);
}
}
}
but the actual result is:
hello
hello
hello
he
llo
I want, if the word doesn't have any letters in the letters_array
, it to print only one time and if the word has a letters in the letters_array
it should divide two 2 part in the location of founded letter as I make in my code.
Thanks.