0
 public class Vowels {
    public static void main(String[] args) {
        char []a={'a','e','i','o','u','e'};
        String line="abcee";
        for (char c : a) {
            for (int i = 0; i < a.length; i++) {
                if (line.charAt(i)==c) {
                    line=line.substring(0, i)+line.substring(i+1);
                }
            }
        }
        System.out.println(line);
    }
}

whats wrong with this code more than same vowels not removed.

Will
  • 6,561
  • 3
  • 30
  • 41
  • 3
    Please provide sample input, expected output and actual output. Explain what you observed when debugging through the code to *try* to work out what's happening. For *starters* it looks like your loop bounds for `i` are very odd... if you have a very long string, why do you only want to look at the first 5 characters? – Jon Skeet Sep 22 '15 at 12:55
  • possible duplicate of [Remove all vowels in a string with Java](http://stackoverflow.com/questions/13167495/remove-all-vowels-in-a-string-with-java) – Blake Yarbrough Sep 22 '15 at 13:09

5 Answers5

2

This code will remove A E I O and U from any String. This code is from Writing a method to remove vowels in a Java String, for a solution using loops see Remove all vowels in a string with Java.

public static void main(String[] args) {
    String line = "abcee";
    
    line = line.replaceAll("[AEIOUaeiou]", "");
    
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    
    alphabet = alphabet.replaceAll("[AEIOUaeiou]", "");
    
    System.out.println(line);
    
    System.out.println(alphabet);
}

OUTPUT:

bc

bcdfghjklmnpqrstvwxyz

Community
  • 1
  • 1
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
1

There are two problems:

  • Switch order of the loops (right now if you have two following identical vowels the second one will not be removed).
  • Change a.length into line.length()

for (int i = 0; i < line.length(); i++)
  for (char c : a)
gawi
  • 2,843
  • 4
  • 29
  • 44
1

Try this (using substring is a bit weird though) :

public class Vowels {
public static void main(String[] args) {
    char []a={'a','e','i','o','u','e'};
    String line="abcee";

    for (char c : a) {
        while(line.indexOf(c) > -1){
            line = line.substring(0, line.indexOf(c)) + line.substring(0, line.indexOf(c)+1); 
        }
    }
    System.out.println(line);
 }

}

pmartin8
  • 1,545
  • 1
  • 20
  • 36
  • I think you are on the right track to the most efficient solution using the `.replaceAll()` method. However, with a more sophisticated regex you can eliminate the need for the loop and perform significantly fewer operations. – Blake Yarbrough Sep 22 '15 at 13:11
  • Yeah, I changed my solution to comply with the requirement of using subString instead. – pmartin8 Sep 22 '15 at 13:15
0

Try this:

public static void main(String[] args){
    String[] a = { "a", "e", "i", "o", "u" };
    ArrayList<String> vowels = new ArrayList<String>();
    vowels.addAll(Arrays.asList(a));
    String line = "abceesdfdsfewereraerwe";
    for (int i = 0; i < line.length(); i++) {
            String val = String.valueOf(line.charAt(i));
            if (vowels.contains(val)) {
                line = line.replace(val, "");
            }
        }
    System.out.println(line);
    }
}
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
Thanigai Arasu
  • 413
  • 3
  • 14
0

You can use Pattern and Matcher

    String str = "abceeuoopqr";
    Pattern pattern = Pattern.compile("[aeioue]");
    Matcher matcher = pattern.matcher(str);
    if(matcher.find()){
        System.out.println(matcher.replaceAll(""));
    }
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44