2013-10-12 33 views
1

元音的方法我是編程的初學者,我寫一個Java方法中刪除字符串的元音,但我不知道如何解決這個錯誤:";" expected寫去除以Java String的

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; 
} 
+1

方法不能在Java中嵌套,將「boolean isVowel(char c)」方法移出「disemvowel」主體。 –

+0

是的,好像你正在試圖使'isVowel'成爲一種方法,但是你已經從多個角度拙劣了。 –

回答

4

一個更簡單的方法是做到以下幾點:

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

這將適用於正則表達式,[AaEeIiOoUu]string。此表達式將匹配字符組[AaEeIiOoUu]中的所有元音,並將其替換爲""空字符串。

+0

他的問題似乎沒有指出或指出避免字邊界。真的,它看起來是他要修復的作業 - 所以完全改變形式不會被正確標記。 –

+0

這不是一個不區分大小寫的解決方案。 –

+0

謝謝。我修復了一些問題 – cmd

14
String str= "Your String"; 
str= str.replaceAll("[AEIOUaeiou]", ""); 
System.out.println(str); 
1

你有語法錯誤很多

  • boolean isVowel(char c); - 不確定你在做什麼。如果你想把它當作一個單獨的方法,它只能分離出(不要將分號後,這將是無效的語法。

  • else if if是無效的語法。如果你正在做一個else if,那麼你需要一個if

  • 即使代碼將編譯,for (int z = 0; z <= l; z++)會使你走下弦。贊成<取出<=
  • isVowel == "false"是從來沒有去上班。你是一個比較將字符串轉換爲布爾值。您想要!isVowel代替。

把語法錯誤放在一邊,可以這樣想。

  • 你有一個包含元音的字符串。你希望有一個不包含元音的字符串。
  • 最直接的方法是迭代字符串,將所有非元音字符放入單獨的字符串中,然後返回。

有趣的是,你在那裏的半方法可以完成確定某物是否爲元音的邏輯。將它解壓到自己的方法中。然後,用其他方法稱爲。儘管考慮大寫字母。

我把剩下的作爲練習留給讀者。

0

你可以嘗試這樣的事:

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(); 
} 
1

這裏是你的代碼,而無需改變任何邏輯,但解讀的isVowel方法:

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

(是的,我知道這應該是一個但不能將格式化的代碼放入註釋中。)

+0

修正了字符串比較的情況。 –

+0

@SotiriosDelimanolis - 我不打算讓它工作,只是解開它來說明如何進行。但顯然OP沒有正確地做他的方法「呼叫」。我會添加評論來指出。 –