【问题标题】:Tracking while reading a text file in Java在 Java 中读取文本文件时进行跟踪
【发布时间】:2014-02-10 07:57:18
【问题描述】:

我正在尝试使用 BufferedReader 构建一个程序,该程序可以读取文件并跟踪元音、单词,并且可以计算每行的平均单词数。我已经准备好读取文件的骨架,但我真的不知道从哪里获取它。任何帮助,将不胜感激。谢谢。

import java.io.*;

public class JavaReader
{
    public static void main(String[] args) throws IOException
    {
        String line;
        BufferedReader in;

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

        line = in.readLine();

        while(line != null)
        {
            System.out.println(line);
            line = in.readLine();
        }


    }

}

【问题讨论】:

  • 请使用 Java 命名约定。课程应该在PascalCase。由于您刚刚开始,我建议您在有很多有问题的课程之前重命名您的课程。
  • @BoristheSpider 感谢您指出这一点。完成。
  • 要跟踪每行的单词量,您可以用空格(和标点符号)分割每一行以获得包含所有单词的数组。然后你只需得到这个数组的大小,就可以知道实际的字数。
  • 使用扫描器标记或字符串标记器是一种更简单的方法。
  • @NicRobertson 关心发布示例?我对 Java 很陌生。

标签: java input output bufferedreader


【解决方案1】:

这就是我得到的。字数统计是有问题的,但我将举一个例子。可以进行更改(我接受批评)。

import java.io.*;

public class JavaReader
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader("message.txt"));
        String line = in.readLine();

        // for keeping track of the file content
        StringBuffer fileText = new StringBuffer();

        while(line != null) {
            fileText.append(line + "\n");
            line = in.readLine();
        }

        // put file content to a string, display it for a test
        String fileContent = fileText.toString();
        System.out.println(fileContent + "--------------------------------");

        int vowelCount = 0, lineCount = 0;

        // for every char in the file
        for (char ch : fileContent.toCharArray())
        {
            // if this char is a vowel
            if ("aeiou".indexOf(ch) > -1) {
                vowelCount++;
            }
            // if this char is a new line
            if (ch == '\n') {
                lineCount++;
            }
        }
        double wordCount = checkWordCount(fileContent);
        double avgWordCountPerLine = wordCount / lineCount;

        System.out.println("Vowel count: " + vowelCount);
        System.out.println("Line count: " + lineCount);
        System.out.println("Word count: " + wordCount);
        System.out.print("Average word count per line: "+avgWordCountPerLine);
    }

    public static int checkWordCount(String fileContent) {

        // split words by puncutation and whitespace
        String words[] = fileContent.split("[\\n .,;:&?]"); // array of words
        String punctutations = ".,:;";
        boolean isPunctuation = false;
        int wordCount = 0;

        // for every word in the word array
        for (String word : words) {

            // only check if it's a word if the word isn't whitespace
            if (!word.trim().isEmpty()) {
                // for every punctuation
                for (char punctuation : punctutations.toCharArray()) {

                    // if the trimmed word is just a punctuation
                    if (word.trim().equals(String.valueOf(punctuation)))
                    {
                        isPunctuation = true;
                    }
                }

                // only add one to wordCount if the word wasn't punctuation
                if (!isPunctuation) {
                    wordCount++;
                }
            }
        }
        return wordCount;
    }
}

示例输入/输出:

文件:

This is a test. How do you do?


This is still a test.Let's go,,count.

输出:

This is a test. How do you do?


This is still a test.Let's go,,count.
--------------------------------
Vowel count: 18
Line count: 4
Word count: 16
Average word count per line: 4.0

【讨论】:

    【解决方案2】:

    您可以使用 Scanner 传递该行并检索字符串行的每个标记。

    line = line.replaceAll("[^a-zA-Z]", ""); //remove all punctuation
    line = line.toLowerCase();               //make line lower case
    Scanner scan = new Scanner(line);
    String word = scan.next();
    

    然后你可以遍历每个标记来计算每个单词中的元音。

    for(int i = 0; i < word.legnth(); i++){
        //get char
        char c = word.charAt(i);
        //check if the char is a vowel here
        if("aeiou".indexOf(c) > -1){
            //c is vowel
        }
    }   
    

    您需要做的就是设置几个计数器整数来跟踪这些,然后您就笑了。

    啊,如果您想确保没有诸如“ - ”之类的非单词算作一个单词,最简单的方法可能是从文本中删除所有非字母数字字符。 上面我也加了。

     line = line.replaceAll("[^a-zA-Z]", "");
     line = line.toLowerCase();
    

    哦,既然你是java新手,别忘了导入

     import java.util.Scanner;
    

    【讨论】:

    • 默认情况下,Scanner 标记在空格上 - 这样hypen就会有一个单词。 OP 可能不希望这样。
    • 一些修剪、一些正则表达式和一些#contains 可以解决这个问题。
    • if("aeiou".indexOf(c) &lt; 0){ 需要修复
    猜你喜欢
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多