2014-12-04 29 views
-1

我正在嘗試在一個音節計數器上計算我輸入的字符串中的音節。這裏是說明。 -SyllableCounter用一個詞來計算音節

Syllables are really useful for a lot of things. They are defined according to the following rules involving 
consonants (non-vowels) and vowels (a, e, i, o, u, y): 
    Starting y omitted: 
     (a) when words begin with y, we don’t count the starting y as a vowel. (we are 
      assuming there are no words that start with y followed immediately by a consonant) 
    Silent e omitted: 
     (a) when e is the last letter of a word, we’ll assume it is silent, unless the word is café or 
      entrée (words borrowed from French). (* we’ll ignore all other words to simplify) 
      For simplification, it may be best to create a new String without this silent e, before 
      checking for more syllables. 
    With the silent-e omitted, one-syllable units: 
     (a) have a single vowel. 
     (b) have two vowels that are the same letter in succession. 
     (c) have two vowels in immediate succession that are ei, ie, ea, ou, ey, ay, oy, uy, ai. 
     (d) have three vowels in immediate succession that are eau, iou (* yes, there are 
      exceptions to this that we are ignoring here). 
    With the silent-e omitted, two-syllable units: 
     (a) two vowels in immediate succession where the vowels are different letters not 
      following the rule above. For instance, oe, io, oi. 
     (b) three vowels in immediate succession not following the rule above where the last 
      vowel is not a silent e. For instance (「eye」) as in 「meyer.」 
Generate a program called SyllableCounter that counts syllables in a word or sentence (maximum one 
line). Assume the user will not use any punctuation. Pseudocode and a testing plan are required. 
Do not print in main(). 

Sample output: 
Please enter your word or sentence, followed by a carriage return. 
Sally where are you going 
Your entry has 7 syllables. 

這裏是我當前的代碼(它編譯,但有一個的StringIndexOutOfBoundsException -

/* 
* SyllableCounter.java 
* Prints the number of syllables based on an inputed string 
* 
* Testing - What should work - All strings with letter characters 
*  What shouldn't work - Number values 
*/ 

import java.util.Scanner; //import the scanner 

public class SyllableCounter //class is SyllableCounter 
{ 

    public static void main (String args[]) //main() method header 
    { 

     String string = ""; 


     string = getInput(); //call getInput() 
     int totalCount = calc(string); //call calc() 
     printOut(totalCount); //call printOut() 

    } 

    public static String getInput() //getInput() method 
    { 
     Scanner console = new Scanner (System.in); //create an instance of the scanner 
     System.out.println("Please enter your word or sentence, followed by a carrige return"); 
     String input = console.nextLine(); //get the inputted string and return it 
     return input; 
    } 

    public static int calc (String string) 
    { 
     //int finalCount = 0; 
     //int index = string.indexOf(' '); 
     return calcWord(string); 

    } 

    public static int calcWord(String word) //calc() method 
    { 

     int count = 0; 
     //for loop goes through all charectors 
     int length = word.length(); 
     for (int i = 0; i<length; i++) 
     { 
      if ((word == "entree") || (word == "cafe")) 
       return 2; 

      else if (i==0)//if i is 0 
      { 
       if (word.charAt(i) == 'a' //if letter is a,e,i,o or u 
         || word.charAt(i) == 'e' 
         || word.charAt(i) == 'i' 
         || word.charAt(i) == 'o' 
         || word.charAt(i) == 'u') 
         count++ ; //count ++ 
       else //else 
        {} //nothing 
      } 
      else if (i==word.length()-1) //else if i is the last letter of the string 
      { 
       if ((word.charAt(i) == 'a') || (word.charAt(i) == 'i') || (word.charAt(i) == 'o') || (word.charAt(i) == 'u') || (word.charAt(i) == 'y')) 
       //else if letter is a,i,o,u or y (also 2 or 3 in a row) 
       { 
        count ++ ;//count ++ 
       } 

       else //else 
        {} //nothing 
      } 

      else if (word.charAt(word.length()-1) == 'e') { 
       if (length >= i+2) 
        if (word.substring(i,i+3) == "eau" 
         || word.substring(i,i+3) == "iou") 
        { 
         count++; 
         i+=2; 
        } 
        else if (word.substring(i,i+2) == "ei" 
          || word.substring(i,i+2) == "ie" 
          || word.substring(i,i+2) == "ea" 
          || word.substring(i,i+2) == "ou" 
          || word.substring(i,i+2) == "ey" 
          || word.substring(i,i+2) == "ay" 
          || word.substring(i,i+2) == "oy" 
          || word.substring(i,i+2) == "uy" 
          || word.substring(i,i+2) == "ai") 
        { 
         count++; 
         i++; 
        } 
        else if(word.substring(i, i+2) == "oe" 
         || word.substring(i, i+2) == "io" 
         || word.substring(i, i+2) == "oi") 
        { 
         count+=2; 
         i++; 
        } 
       } 
       else { 
        if (word.charAt(i) =='a' 
          || word.charAt(i) == 'e' 
          || word.charAt(i) == 'i' 
          || word.charAt(i) == 'o' 
          || word.charAt(i) == 'u') 
        { 
         count++; 
        } 
       } 


     if (!(word.charAt(word.length()-1) == 'e')) 
     { 
       if (word.substring(i,i+3) == "eau") 
       { 
         count++; 
         i+=2; 
       } 
       else if (word.charAt(i) == 'a' 
         || word.charAt(i) == 'e' 
         || word.charAt(i) == 'i' 
         || word.charAt(i) == 'o' 
         || word.charAt(i) == 'u') 
       { 
        count++; 
       } 
      } 
      else if (word.charAt(i) == 'a' 
        || word.charAt(i) == 'e' 
        || word.charAt(i) == 'i' 
        || word.charAt(i) == 'o' 
        || word.charAt(i) == 'u') 
      { 
       count++; 
      } 
      else //else 
       {} //nothing 
     } 
     return count;//return the count 
    } 

    public static void printOut(int count) //printOut() method 
    { 
     System.out.println(count); 
     // print the count 
    } 
} 
+0

這種異常發生在哪裏? – August 2014-12-04 03:52:55

回答

3

我用這個詞 「富」 輸入到你的程序:

這裏(大約線112對我來說):

if (!(word.charAt(word.length()-1) == 'e')){ 

    if (word.substring(i,i+3) == "eau") 

上面的代碼段失敗的一個例子是使用單詞foo :

字長-1的字符不是e,因此上面的條件將被評估。然而,當我等於1時,i + 3將等於4.所以,4在「foo」的長度之外導致錯誤。

請記住,這只是這類故障的一個例子。確保無論何時從i到i + n獲得子字符串,正在評估的字符串都有i + n個字符。

0

手動尋找解決方案是很多工作。正則表達式可以成爲解決您的問題的最佳選擇。看看下面的鏈接:

How to calculate syllables in text with regex and Java

而且提醒的是,上述參考不列入考慮Ÿ作爲一個音節,你將不得不調整正則表達式一點得到所需的輸出。加上元音「e」需要做一些檢查才能得到確切的結果。