1

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.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
KimoKono
  • 91
  • 2
  • 5

2 Answers2

0

I renamed letters_array to lettersArr and words_array to wordsArr but I believe you want a Set of String(s); for example,

// TreeSet would be ordered.
Set<String> wordSet = new HashSet<String>(); // The set of words.
for (int i = 0; i < wordsArr.length; i++) {
  final String word = wordsArr[i]; // local copy of the word.
  for (int j = 0; j < lettersArr.length; j++) {
    int pos = word.indexOf(lettersArr[j]); // get the position of the letter.
    if (pos > -1) { // if the word contains the letter.
      final String first = word.substring(0, pos)
          .trim();
      final String second = word.substring(
          pos + 1, word.length()).trim();
      if (first.length() > 0) {
        wordSet.add(first); // add before the letter.
      }
      if (second.length() > 0) {
        wordSet.add(second); // add after the letter.
      }
    } else {
      wordSet.add(word); // add the word.
    }
  }
}
System.out.println(wordSet); // display the result.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks for help but your code made the same problem which is print the word one more time till divide it . as i shown in my first post – KimoKono Dec 09 '13 at 11:27
0

The most concise way is to split the string by Regex:

public static void main(String... args){
    String[] word_array = {"blahXblahX", "blahYblahY", "blahZblahZ"};
    char[] letters_array = {'X','Z'};

    String pattern = "(?<=[" + new String(letters_array) + "])";
    for(int i=0; i<word_array.length; i++)
        for(String str: word_array[i].split(pattern))
            System.out.println(str);
}

Output:

blahX
blahX
blahYblahY
blahZ
blahZ

Explanation:

For this example pattern will have the form (?<=[XZ]) which matches on the delimiters X and Z but will not consume the delimiters. For example:

// consumes delimiter: ["blah","blah"]
"blahXblahX".split("[XZ]");

// keeps delimiter: ["blahX","blahX"]
"blahXblahX".split("(?<=[XZ])");
Community
  • 1
  • 1
bcorso
  • 45,608
  • 10
  • 63
  • 75
  • Thanks for help but i don't prefer regular expression in my code I cant handle it as exactly i want – KimoKono Dec 09 '13 at 11:26
  • Do you mean that the above code does not do what you want? Or that you don't know (and don't want to learn) regular expressions? – bcorso Dec 09 '13 at 16:49
  • No my frind it helps me but it solve a small part in my case and i cant edit on it . – KimoKono Dec 10 '13 at 04:48