我需要计算文本文档中单词的总数,每行的平均单词数和每行的元音

时间:2015-05-19 22:03:01

标签: java string algorithm io bufferedreader

我对java比较新,并且对我正在处理的程序有疑问。该程序的主要思想是给出一个文件,计算文件中的单词总数,每行的平均单词数和每行的元音。 我提出了一个起点,但我真的不知道如何继续。

import java.io.*;

public class Example {
    public static void main(String[] args) throws IOException {
        int vowelCount = 0, wordsAvg = 0, wordsCount = 0, a;
        char ch;
        String line;
        int vowels1 = 0, vowels2 = 0, vowels3 = 0, vowels4 = 0, vowels5 = 0, vowels6 = 0, vowels7 = 0, vowels8 = 0, vowels9 = 0, vowels10 = 0;
        BufferedReader in ;

        in = new BufferedReader(new FileReader("message.txt"));

        line = in .readLine();
    }
    for (int i = 0; i < line.length(); i++) {
        ch = line.charAt(i);

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels1++;
    }

    {
        System.out.println(line);
        System.out.println("--------------------------------------------");
        System.out.println("Total number of words: " + wordsCount);
        System.out.println("Average number of words per line: " + wordsAvg);
        System.out.println("The number of vowels in line 1 is: " + vowels1);
        System.out.println("The number of vowels in line 2 is: " + vowels2);
        System.out.println("The number of vowels in line 3 is: " + vowels3);
        System.out.println("The number of vowels in line 4 is: " + vowels4);
        System.out.println("The number of vowels in line 5 is: " + vowels5);
        System.out.println("The number of vowels in line 6 is: " + vowels6);
        System.out.println("The number of vowels in line 7 is: " + vowels7);
        System.out.println("The number of vowels in line 8 is: " + vowels8);
        System.out.println("The number of vowels in line 9 is: " + vowels9);
        System.out.println("The number of vowels in line 10 is: " + vowels10);
        line = in .readLine();
    }
}

到目前为止,我有程序读取文件并计算第一行的元音,但我真的不知道如何遍历每一行。 此外,我需要输出显示,因为我有预先设置。 任何帮助都是值得赞赏的,试图尽可能保持简单。

4 个答案:

答案 0 :(得分:2)

您可以使用BufferedReader在java中读取文件,直到line不为空。在阅读文件时,您可以调用适当的方法来计算单词,元音等的数量 -

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    try {

        String line = br.readLine();

        while (line != null) {
            int wordsPerLine = countWord(line);
            int vowelsPerLine = countVowel(line);

            line = br.readLine();
        }

    } finally {
        br.close();
    }  

现在您可以像这样编写countWord(String line)方法 -

//this method will count word for per input line
public int countWords(String line)
{
   String words[]=line.split(" ");
   int count=words.length;
   return count;
}  

你也可以像这样计算每行的元音 -

public int coutnVowels(String line){

   int count =0;
   for(int i=0; i<line.length; i++){

      char currentChar = line.CharAt(i);
      if(currentChar=='a'||currentChar=='e'||currentChar=='i'||currentChar=='o'
      currentChar=='u'){

         count++;
      }
   }

   return count;
}

答案 1 :(得分:1)

循环浏览文件

BufferedReader br;
String line = br.readLine();  
while (line != null)  
{  
   ... // do stuff to file here  
   line = br.readLine();  
} 

答案 2 :(得分:1)

<强> EDITED

Scanner myScanner = new Scanner(new File(fileName));
int nbLines = 0, nbWords = 0, nbVowels = 0;

while(myScanner.hasNextLine()) {
line = myScannner.nextLine();
    nbWords += countWords(line); // the method in Razib's post
    ++nblines;  // increment number of lines
    nbVowels += coutnVowels(line); // again use the method Razib gave you.
}
myScanner.close();

//nbWords now contains the number of words in the file.
// nblines is the number of line in the file.
// and nbVowels the total number of vowels in the file.

int avrg = nbWords / nblines; //average words per line.

这应该可行。

你应该能够自己做其余的事情;)

答案 3 :(得分:1)

另一个提示是使用正确的数据类型来存储每一行​​的元音。

由于我们不知道文件中有多少行,因此ArrayList很有用:

int lineNo = 1;
for(Integer nrVowels : vowels) {
    System.out.println("The number of vowels in line " 
                       + lineNo + " is: "+ nrVowels);
}

这将存储数组列表中每一行的元音。

然后显示你在元音数组列表上循环的计数,循环遍历arraylist:

{"status":0, "environment":"Sandbox", "receipt": 
{"receipt_type":"ProductionSandbox", "adam_id":0, "app_item_id":0, 
"bundle_id":"xxxxxxxxx", "application_version":"0", 
"download_id":0, "version_external_identifier":0, 
"request_date":"2015-05-19 22:04:45 Etc/GMT",  
"request_date_ms":"1432073085427",
"request_date_pst":"2015-05-19 15:04:45 America/Los_Angeles",
"original_purchase_date":"2013-08-01 07:00:00 Etc/GMT", 
"original_purchase_date_ms":"1375340400000", 
"original_purchase_date_pst":"2013-08-01 00:00:00 America/Los_Angeles",
"original_application_version":"1.0", "in_app":[ {"quantity":"1", 
"product_id":"xxxxxx", "transaction_id":"xxxxxxxxx", 
"original_transaction_id":"xxxxxxxx", "purchase_date":"2015-05-19 18:32:54 Etc/GMT", "purchase_date_ms":"1432060374000", 
"purchase_date_pst":"2015-05-19 11:32:54 America/Los_Angeles", 
"original_purchase_date":"2015-05-19 18:32:54 Etc/GMT", 
"original_purchase_date_ms":"1432060374000",
"original_purchase_date_pst":"2015-05-19 11:32:54 America/Los_Angeles", "is_trial_period":"false"}]}}