-3

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));
6
  • 1
    And what's the problem? Commented Jul 8, 2014 at 12:09
  • you seem to be missing some parantheses in the for loop Commented Jul 8, 2014 at 12:09
  • 4
    What about a simpler dalek[i] = dalek[i].replaceAll("[aeiou]","%20");? Commented Jul 8, 2014 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. Commented Jul 8, 2014 at 12:13
  • 1
    @Sulthan well, indentation might help, but ask Apple (ok, it wasn't a break but goto) ... ;) Commented Jul 8, 2014 at 12:24

4 Answers 4

5

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]
Sign up to request clarification or add additional context in comments.

2 Comments

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

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));

Comments

0

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));

Comments

0
    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

1 Comment

It gives output as expected

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.