2016-09-26 137 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