2013-12-09 127 views
0

我需要編寫一個接受字符串的程序,並打印出除去所有元音除外的字符串文本,除非一個字以它開頭。需要打印沒有元音的掃描儀字符串的代碼

我寫的代碼是一半的,但我不明白爲什麼它不會返回整個字符串,並且不會刪除所有的元音。說我輸入短語「理想的財產區」。該程序打印字符串,「Dirlp」而不是「Dsrbl prprty ar」

有人可以告訴我如何改善代碼,使其工作?謝謝!

這裏是我的代碼:

public static void main (String [] args) 
{ 
    Scanner in = new Scanner (System.in); 
    System.out.print ("Enter some text, then hit enter: "); 
    String text = in.nextLine(); 

    takeOutVowel (text); 
    System.out.println(); 
} 

static void takeOutVowel (String s) 
{ 
    char ch = s.charAt(0);        //character to be printed 
    System.out.print (ch); 
    int nextCh = 1;         //determines the position of the next character 
    int increase = 1;        //increase is how much i will increase by in the for loop; takes the position of vowels into              //consideration so they can be skipped 

    for (int i = 1; i <= s.length(); i += increase) 
    { 
     ch = s.charAt (nextCh); 

     if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 
     { 
      nextCh++; 
      ch = s.charAt (nextCh); 

      while (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); 
      { 
       nextCh++; 
       ch = s.charAt (nextCh); 

       if (nextCh >= s.length()) 
       { 
        ch = ' '; 
        break;   
       } 

      } 
     } 

     System.out.print (ch); 
     nextCh++; 
     ch = s.charAt (nextCh); 

     if (ch == ' ')         //if the previous position was a space, then this will allow for the vowel to be printed 
     { 
      System.out.print ("" + ch + s.charAt(nextCh + 1)); 
      nextCh++; 
     } 

     increase = nextCh; 


    } 

感謝所有的答案迄今 - 非常有幫助!我不允許使用數組或任何未覆蓋的內容,所以我已將代碼修改爲下面的內容。它編譯罰款,但是當我運行該程序並進入掃描儀的文字,我得到一個消息,說

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 27 
at java.lang.String.charAt(String.java:686) 
at Vowels.noVowels(Vowels.java:20) 
at Vowels.main(Vowels.java:11) 

我想不出是什麼問題了。再次感謝您的幫助!

import java.util.*; 

class Vowels 
{ 
    public static void main (String [] args) 
    { 
     Scanner in = new Scanner (System.in); 
     System.out.print ("Enter some text, then hit enter: "); 
     String text = in.nextLine(); 

     System.out.println (noVowels(text)); 
    } 

    static String noVowels (String s) 
    { 
     String noVowels = "" + s.charAt(0);     //Starts a new string with the first character of the input text 

     for (int i = 1; i <= s.length(); i++) 
     { 
      if (isVowel(s.charAt(i)) && s.charAt(i-1) != ' ')  //if the character is a vowel and it's previous character wasn't a space, then this is a vowel to be            replaced 
      { 
       noVowels = noVowels.concat(""); 
      } 

      else 
      { 
       noVowels = noVowels.concat("" + s.charAt(i)); 
      } 
     } 

     return noVowels; 
    } 

    static boolean isVowel (char ch) 
    { 
     if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 
     { 
      return true; 
     } 

     else 
     { 
      return false; 
     } 
    } 
} 
+0

http://stackoverflow.com/questions/4430112/how-do-i-remove-some-characters-from-my-string(我的意思是,你可以只是與每一個元音一個簡單的正則表達式。 ..) – Shiki

+0

如果單詞以元音開頭,那麼您不需要從該單詞中刪除任何元音? – Adarsh

+0

我假設你的索引超出範圍是因爲在'for'循環中你的條件是'i <= s.length();'。假設你的字符串是「hello」 - 長度是5,但是's.charAt(5)'不存在。陣列中的最大位置是4. –

回答

1

你可以很容易地解決這個問題。迭代String並檢查元音。如果沒有元音比追加結果。嘗試

static void takeOutVowel(String s) { 
    StringBuilder res = new StringBuilder(); 
    String[] words = s.split(" +"); 
    for (String word : words) { 
     res.append(word.charAt(0)); //Skip the first char 
     for (int i = 1; i < word.length(); i++) { 
      char ch = word.charAt(i); 
      if (!isVowel(ch)) { // Check the vowel 
       res.append(ch); 
      } 
     } 
     res.append(' '); 
    } 

    System.out.println(res); 
} 

static boolean isVowel(char ch){ 
    ch=Character.toLowerCase(ch); // Make it case-insensitive. 
return ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u'; 
} 
+0

這非常有幫助,謝謝!我不允許使用數組是問題所在。有其他解決方法嗎? – srsarkar7

1

您可以使用正則表達式輕鬆完成此操作。第一個例子演示瞭如何簡單地刪除元音。

public static void main(String[] args) 
{ 
    String noVowels = takeOutVowel("Hello how are you?"); 
    System.out.println(noVowels); // prints "Hll hw r y?" 
} 

// This will remove all vowels from any String 
private static String takeOutVowel(String s) 
{ 
    return s.replaceAll("[aeiou]", ""); 
} 

但現在,以滿足您忽略一個單詞的第一個字母的要求,如果它是一個元音(這意味着我們會忽略它,不管是什麼),你只需要編輯takeOutVowel方法的位。

static String takeOutVowel (String s) 
{ 
    // split your string so we can examine each word separately 
    String[] words = s.split(" "); 
    String noVowels = ""; 

    for (int i = 0; i < words.length; i++){ 
     char firstChar = words[i].charAt(0); 
     String temp = words[i].substring(1, words[i].length()).replaceAll("[aeiou]", ""); 
     noVowels += firstChar + temp + " "; 
    } 

    return noVowels; 
} 
+0

如何跳過第一個字符?根據OP的要求,結果應該像'Hll hw ar y?' – Masudul

+0

@Masud你是對的,我盯着那個要求。我會寫點東西,並編輯我的答案。感謝您的注意! –

+0

@ adback03非常感謝您的幫助!我不允許爲這個程序使用數組。你知道另一種方法嗎?再次感謝 – srsarkar7

0

這適用於您的以下要求 - 除了當單詞以元音開始時,刪除所有元音。

import java.util.Scanner; 

public class Test { 

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 
    System.out.print("Enter some text, then hit enter: "); 
    String text = in.nextLine(); 
    takeOutVowel(text); 
    System.out.println(); 
} 

static void takeOutVowel(String s) { 
    String[] array = s.split(" ");       // Split sentence into words and store them in an array. 
    StringBuilder finalString = new StringBuilder(); 
    for (String word : array) {        //For each word in the array 
     char ch = word.toLowerCase().charAt(0);    //check if the lower case first character is a vowel. 
     if(ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u'){ // When it is not, replace all the vowels. 
      finalString = finalString.append(word.replaceAll("[aeiou]", "")).append(" "); 
     }else{ 
      finalString = finalString.append(ch).append(word.replaceAll("[aeiou]", "")).append(" ");// When it is , replace all the vowels except the first character. 
     } 
    } 
    System.out.println(finalString); 
} 
}