【问题标题】:I need to count the total amount of words, average number of words per line and vowels per line of a text document我需要计算文本文档的总字数、每行平均字数和每行元音
【发布时间】:2015-07-31 22:45:46
【问题描述】:

我对 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();
    }
}

到目前为止,我有程序读取文件并计算第一行的元音,但我真的不知道如何循环遍历另一行。 我还需要显示输出,因为我已经预先设置了它。 感谢您提供任何帮助,尽量使其尽可能简单。

【问题讨论】:

    标签: java string algorithm io bufferedreader


    【解决方案1】:

    循环文件

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

    【讨论】:

      【解决方案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;
      }
      

      【讨论】:

      • 这是让我感到困惑的部分,我不确定如何将它们放在一起。
      【解决方案3】:

      已编辑

      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.
      

      这应该可行。

      你应该可以自己做剩下的事情;)

      【讨论】:

        【解决方案4】:

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

        因为我们不知道文件中有多少行,所以 ArrayList 会很有用:

        import java.util.ArrayList;
        
        ArrayList<Integer> vowels = new ArrayList<Integer>()
        
        while (line != null) {
        
        
            vowels.add(countVowels(line));
        
        } 
        

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

        然后要显示循环遍历元音数组列表的计数,循环遍历数组列表:

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

        【讨论】:

        • 我是否应该将某些内容替换为 的位置?因为它似乎不喜欢 ">" 和 "
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-07
        • 1970-01-01
        • 2021-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多