2016-09-26 134 views
2

我正在創建一個從文本文件中讀取元音的程序。該文本是一段很長的,我希望程序每個句子計算元音。從文本文件中讀取元音

所以這是一個例子 7元音

另一個 3元音

到目前爲止,我寫的代碼,以便能夠讀取元音。儘管如此,它將其視爲一個額外的整體。在循環中,它會先計數7,然後第二行將輸出爲10.我希望它輸出7作爲第一行和3作爲第二行。

我正在查看從Java的字符串API,我沒有看到任何可以幫助解決這個問題。我目前對元音進行計數的方式是使用for循環來循環Charat()。我是否錯過了一些東西,或者有沒有辦法阻止它讀取並添加到櫃檯?

下面是一個例子

while(scan.hasNext){ 
     String str = scan.nextLine(); 
     for(int i = 0; i<str.length(); i++){ 
     ch = str.charAt(i); 
     ... 
     if(...) 
      vowel++; 
     }//end for 
     S.O.P(); 
     vowel = 0;//This is the answer... Forgotten that java is sequential... 
     } 

    }// end main() 
    }//end class 

    /*output: 
    This sentence have 7 vowels. 
    This sentence have 3 vowels. 
    */ 
+0

您可以發佈您的代碼? –

+0

你爲什麼不發佈你的代碼? – passion

+0

你想讓它計算每句話還是每行? – afsafzal

回答

2

我創建了一個簡單的類來實現什麼,我相信你的目標是。元音重新設置,以便你沒有提到的句子元音相互加入的問題。我假設通過查看我的代碼,你可以看到你自己的解決方案?此外,這段代碼假設你包含「y」作爲元音,並且它還假定句子以適當的標點符號結尾。

public class CountVowels{ 
    String paragraph; 
    public CountVowels(String paragraph){ 
     this.paragraph = paragraph; 
     countVowels(paragraph); 
    } 

    int vowelTotal = 0; 
    int sentenceNumber = 0; 
    public void countVowels(String paragraph){ 
     for(int c = 0; c < paragraph.length(); c++){ 
      if(paragraph.charAt(c) == 'a' || paragraph.charAt(c) == 'e' || paragraph.charAt(c) == 'i' || paragraph.charAt(c) == 'o' || paragraph.charAt(c) == 'u' || paragraph.charAt(c) == 'y'){ 
       vowelTotal++; //Counts a vowel 
      } else if(paragraph.charAt(c) == '.' || paragraph.charAt(c) == '!' || paragraph.charAt(c) == '?'){ 
       sentenceNumber++; //Used to tell which sentence has which number of vowels 
       System.out.println("Sentence " + sentenceNumber + " has " + vowelTotal + " vowels."); 
       vowelTotal = 0; //Resets so that the total doesn't keep incrementing 
      } 
     } 
    } 
} 
+0

你還有另外一個主要的假設。嘗試一下這段話:「我向我的教授布萊克博士展示了我的解決方案,但她告訴我這存在問題。」 – ajb

+0

哇,我沒有想到這一點。謝謝你指出。 –

+0

是的 - 但我真的不知道一個好的解決方案,除非假設沒有任何縮寫。 – ajb

1

也許不是最優雅的方式,但真正的快速計算每個句子元音我想出了這個,測試和工程(至少在我的測試字符串):

String testString = ("This is a test string. This is another sentence. " + 
      "This is yet a third sentence! This is also a sentence?").toLowerCase(); 
    int stringLength = testString.length(); 
    int totalVowels = 0; 
    int i; 

     for (i = 0; i < stringLength - 1; i++) { 
      switch (testString.charAt(i)) { 
       case 'a': 
       case 'e': 
       case 'i': 
       case 'o': 
       case 'u': 
        totalVowels++; 
        break; 
       case '?': 
       case '!': 
       case '.': 
        System.out.println("Total number of vowels in sentence: " + totalVowels); 
        totalVowels = 0; 
      } 

     } 

    System.out.println("Total number of vowels in last sentence: " + totalVowels); 
1

這裏是一個完整的例子來計算文件每個句子中元音的數量。它使用了一些先進的技術:(1)正則表達式將段落分解成句子;和(2)HashSet數據結構來快速檢查一個字符是否是元音。該程序假定文件中的每一行都是段落。

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Arrays; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 

public class CountVowels { 

    // HashSet of vowels to quickly check if a character is a vowel. 
    // See usage below. 
    private Set<Character> vowels = 
     new HashSet<Character>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'y')); 

    // Read a file line-by-line. Assume that each line is a paragraph. 
    public void countInFile(String fileName) throws IOException { 

     BufferedReader br = new BufferedReader(new FileReader(fileName)); 
     String line; 

     // Assume one file line is a paragraph. 
     while ((line = br.readLine()) != null) { 
      if (line.length() == 0) { 
       continue; // Skip over blank lines. 
      } 
      countInParagraph(line); 
     } 

     br.close(); 
    } 

    // Primary function to count vowels in a paragraph. 
    // Splits paragraph string into sentences, and for each sentence, 
    // counts the number of vowels. 
    private void countInParagraph(String paragraph) { 

     String[] sentences = splitParagraphIntoSentences(paragraph); 

     for (String sentence : sentences) { 
      sentence = sentence.trim(); // Remove whitespace at ends. 
      int vowelCount = countVowelsInSentence(sentence); 
      System.out.printf("%s : %d vowels\n", sentence, vowelCount); 
     } 
    } 

    // Splits a paragraph string into an array of sentences. Uses a regex. 
    private String[] splitParagraphIntoSentences(String paragraph) { 
     return paragraph.split("\n|((?<!\\d)\\.(?!\\d))"); 
    } 

    // Counts the number of vowels in a sentence string. 
    private int countVowelsInSentence(String sentence) { 

     sentence = sentence.toLowerCase(); 

     int result = 0;  
     int sentenceLength = sentence.length(); 

     for (int i = 0; i < sentenceLength; i++) { 
      if (vowels.contains(sentence.charAt(i))) { 
       result++; 
      } 
     } 

     return result; 
    } 

    // Entry point into the program. 
    public static void main(String argv[]) throws IOException { 

     CountVowels cw = new CountVowels(); 

     cw.countInFile(argv[0]); 
    } 
} 

此文件example.txt中:

So this is an example. Another. 

This is Another line. 

下面是結果:

% java CountVowels example.txt 
So this is an example : 7 vowels 
Another : 3 vowels 
This is Another line : 7 vowels