Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wanted to replace all the vowels from the words Apple and Lenovo. Something like this as an output. %20ppl%20, L%20n%20v%20 Here's my code

String[] dalek = new String[]{"Apple","Lenovo"};
String[] wy = new String[]{"a","e","i","o","u"};

for (int i = 0 ; i < dalek.length; i++ )
String wyx = wy[i];

dalek[i] = dalek[i].replace(wyx,"%20");

System.out.println(Arrays.toString(dalek));
share|improve this question

closed as unclear what you're asking by Rafael Winterhalter, Mez, halfelf, George Bailey, Stewie Griffin Jul 8 '14 at 13:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1  
And what's the problem? –  Thomas Jul 8 '14 at 12:09
    
you seem to be missing some parantheses in the for loop –  aniri Jul 8 '14 at 12:09
4  
What about a simpler dalek[i] = dalek[i].replaceAll("[aeiou]","%20");? –  Thomas Jul 8 '14 at 12:11
2  
Just a side node: problems like this (and others like the double break; in iOS SSL code) are the reason why at our company we always use curly braces, even for one-line statements. –  Thomas Jul 8 '14 at 12:13
1  
@Sulthan well, indentation might help, but ask Apple (ok, it wasn't a break but goto) ... ;) –  Thomas Jul 8 '14 at 12:24

4 Answers 4

up vote 5 down vote accepted

Your loop has no curly brackets.

This means only String wyx = wy[i]; will be executed in a loop, not the rest.

Here's a more elegant solution:

String[] dalek = new String[]{"Apple","Lenovo"};
String[] replaced = new String[dalek.length];
for (int i = 0; i < dalek.length; i++) {
    // assigning repalced[i] 
    //            | with dalek at index i
    //            |                      | case insensitive    
    //            |                      |   | vowels class (add "y" if necessary)
    //            |                      |   |         | with URL-encoded space
    replaced[i] = dalek[i].replaceAll("(?i)[aeiou]", "%20");
}
System.out.println(Arrays.toString(replaced));

Output

[%20ppl%20, L%20n%20v%20]
share|improve this answer
    
Not very elegant. The dalek[i++] should be declared as a separate variable. Otherwise it's just another dangeours sideeffect, hard to read. Also, when you need the iterator, then for (int i; ...) would be more elegant - declaring i in the outer scope is not pretty. –  Sulthan Jul 8 '14 at 12:24
2  
@Sulthan yep you caught me while I was editing already. I went with fast enumeration by habit, in this case a normal for loop is more suitable (see edit now). –  Mena Jul 8 '14 at 12:26

This should work:

String[] dalek = new String[]{"Apple","Lenovo"};
String[] wy = new String[]{"a","e","i","o","u"};

for (int i = 0 ; i < dalek.length; i++ ) {
   for (int j=0; j<wy.length;j++){
      String wyx = wy[j];
      dalek[i] = dalek[i].toLowerCase().replace(wyx,"%20");
   }
}

System.out.println(Arrays.toString(dalek));
share|improve this answer

This may help

String[] dalek = new String[] { "Apple", "Lenovo" };
String[] wy = new String[] { "a", "e", "i", "o", "u" };

for (int i = 0; i < dalek.length; i++) {
    for (int j = 0; j < wy.length; j++) {
        String wyx = wy[j];
        dalek[i] = dalek[i].replaceAll("(?i)[" + wyx + "]", "%20");
    }
}
System.out.println(Arrays.toString(dalek));
share|improve this answer
    String[] dalek = new String[]{"Apple","Lenovo"};
    String[] wy = new String[]{"a","e","i","o","u"};

    for (int i = 0 ; i < dalek.length; i++ )
    {
        for(int j=0;j<wy.length;j++)
        {
            dalek[i] = dalek[i].replaceAll("(?i)"+wy[j], "%20");
        }
    }

    System.out.println(Arrays.toString(dalek));

This works

share|improve this answer
    
It gives output as expected –  user2629427 Jul 8 '14 at 13:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.