3

I am a beginner of programming, and am writing a Java method to remove vowel in Strings, but I do not know how to fix this error: ";" expected :

public String disemvowel(String s) {

    boolean isVowel(char c);
    if (c == 'a') {
        return true;
    } else if if (c == 'e') {
        return true;
    } else if if (c == 'i') {
        return true;
    } else if if (c == 'o') {
        return true;
    } else if if (c == 'u') {
        return true;
    }
    String notVowel = "";
    int l = s.length();
    for (int z = 0; z <= l; z++) {


        if (isVowel == "false") {
            char x = s.charAt(z);
            notVowel = notVowel + x;
        }
    }
    return notVowel;
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
user2864813
  • 87
  • 1
  • 3
  • 7
  • 1
    Methods are not nestable in Java, move the "boolean isVowel (char c)" method out of the body of "disemvowel". – Thomas W Oct 12 '13 at 01:04
  • Yep, it seems like you're trying to make `isVowel` a method, but you've bungled that from several standpoints. – Hot Licks Oct 12 '13 at 01:06

5 Answers5

21
 String str= "Your String";
 str= str.replaceAll("[AEIOUaeiou]", "");
 System.out.println(str);
Jace Rhea
  • 4,982
  • 4
  • 36
  • 55
Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
8

A much simpler approach would be to do the following:

String string = "A really COOL string";
string = string.replaceAll("[AaEeIiOoUu]", "");
System.out.println(string);

This will apply the regular expression, [AaEeIiOoUu] to string. This expression will match all vowels in the character group [AaEeIiOoUu] and replace them with "" empty string.

cmd
  • 11,622
  • 7
  • 51
  • 61
1

You've got a lot of syntax errors.

  • boolean isVowel(char c); - not sure what you're doing with this. if you want it as a separate method, separate it out (and don't place a semicolon after it, which would be invalid syntax.

  • else if if is invalid syntax. If you're doing an else if, then you only need the one if.

  • Even if the code would compile, for (int z = 0; z <= l; z++) will cause you to step off of the String. Remove the <= in favor of <.
  • isVowel == "false" is never going to work. You're comparing a String to a boolean. You want !isVowel instead.

Putting the syntax errors aside, think of it like this.

  • You have a string that contains vowels. You wish to have a string that doesn't contain vowels.
  • The most straightforward approach is to iterate over the String, placing all non-vowel characters into a separate String, which you then return.

Interestingly enough, the half-method you have there can accomplish the logic of determining whether something is or isn't a vowel. Extract that to its own method. Then, call it in your other method. Do take into account capital letters though.

I leave the rest as an exercise to the reader.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

Here is your code, without changing any logic, but unscrambling the isVowel method:

public String disemvowel(String s) {

    // Removed the "isVowel" method from here and moved it below

    String notVowel = "";
    int l = s.length();
    for (int z = 0; z <= l; z++) {
        // Note that the "isVowel" method has not been called.
        // And note that, when called, isVowel returns a boolean, not a String.
        // (And note that, as a general rule, you should not compare strings with "==".)
        // So this area needs a lot of work, but we'll start with this
        boolean itIsAVowel = isVowel(s.charAt(z));
        // (I made the variable name "itIsAVowel" to emphasize that it's name has nothing to do with the method name.
        // You can make it "isVowel" -- the same as the method -- but that does not in any way change the function.)
        // Now take it from there...
        if (isVowel == "false") {
            char x = s.charAt(z);
            notVowel = notVowel + x;
        }
    }
    return notVowel;
}

// You had this line ending with ";"
boolean isVowel(char c) {
    if (c == 'a') {
        return true;
   // Note that you coded "if if" on the lines below -- there should be only one "if" per line, not two        
    } else if if (c == 'e') {
        return true;
    } else if if (c == 'i') {
        return true;
    } else if if (c == 'o') {
        return true;
    } else if if (c == 'u') {
        return true;
    }
    // You were missing this final return
    return false;
}

(Yes, I know this should be a comment, but you can't put formatted code in a comment.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Fix the string comparison while you are at it. – Sotirios Delimanolis Oct 12 '13 at 01:16
  • @SotiriosDelimanolis -- I did not intend to make it work, simply unravel it to illustrate how to proceed. But clearly the OP is not doing his method "calls" at all correctly. I'll add comments to point that out. – Hot Licks Oct 12 '13 at 01:38
0

You could try something like this:

public static String removeVowels(final String string){
    final String vowels = "AaEeIiOoUu";
    final StringBuilder builder = new StringBuilder();
    for(final char c : string.toCharArray())
        if(vowels.indexOf(c) < 0)
            builder.append(c);
    return builder.toString();
}
Josh M
  • 11,611
  • 7
  • 39
  • 49