简体   繁体   English

用于计算文本文件中的字符、单词和行数的 Java 程序

[英]Java program to count characters, words, and lines from a text file

I am trying to write a program that gathers statistics on a text file in a way that is similar to word processors which tell you how many characters, words, and lines you have written.我正在尝试编写一个程序,以类似于文字处理器的方式收集文本文件的统​​计信息,它会告诉您您编写了多少个字符、单词和行。

The purpose of this program is to ask the user for the name of a file ( using a scanner ) and output the statistics.该程序的目的是向用户询问文件名(使用扫描仪)并输出统计信息。

Here is what I have so far:这是我到目前为止所拥有的:

public class DocStats {
private File inputFile;
private Scanner in;

// Sets users input (string attribute) to a file name
public DocStats(String string) throws FileNotFoundException {
    try {
        File inputFile = new File(string);
        Scanner in = new Scanner(inputFile);

        this.inputFile = inputFile;
        this.in = in;
    } catch (IOException exception) {
        System.out.println("Could not open the file");
    }
}

// Gets the number of characters in a text
public int getNumberOfCharacters() {
    int numChar = 0;

    while (in.hasNext()) {
        String line = in.nextLine();
        numChar += line.length();
    }

    return numChar;
}

// Gets the number of words in a text
public int getNumberOfWords() {
    int numWords = 0;

    while (in.hasNextLine()) {
        String line = in.nextLine();
        numWords += new StringTokenizer(line, " ,").countTokens();
    }

    return numWords;
}

// Gets the number of lines in a text
public int getNumberOfLines() {
    int numLines = 0;
    while (in.hasNextLine()) {
        numLines++;
    }
    return numLines;
}
}

After I test my class in the main method, I do not get correct outputs.在 main 方法中测试我的类后,我没有得到正确的输出。 Here is the main method:下面是主要方法:

import java.io.*;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
    DocStats doc = new DocStats("goblin.txt");

    System.out.println("Number of characters: "
            + doc.getNumberOfCharacters()); // outputs 1402, instead of 1450

    System.out.println("Number of words: " + doc.getNumberOfWords()); // outputs
                                                                        // 0
                                                                        // instead
                                                                        // of
                                                                        // 257
    System.out.println("Number of lines: " + doc.getNumberOfLines()); // outputs
                                                                        // 0
                                                                        // instead
                                                                        // of
                                                                        // 49
}
}

Could anyone point out why my code does not work and suggest any alternative way to fix it?谁能指出为什么我的代码不起作用并建议任何替代方法来修复它?

You are scanning the file in the getNumberOfCharacters() method, and that is why it is non-zero.您正在 getNumberOfCharacters() 方法中扫描文件,这就是它非零的原因。 The others are 0 since the scanner is at the end of the file.其他为 0,因为扫描仪位于文件末尾。

You need to change your DocStats class to have only one loop您需要将 DocStats 类更改为只有一个循环

in = new Scanner(file);
while(in.hasNextLine()) {
  // count the lines
  lines++;
  String line = in.nextLine();

  // chars in the line, plus one for the newline
  chars  += line.length() + 1;

  // count words.  Are there other word terminators, such as . ? ! -
  words += new StringTokenizer(line, " ,").countTokens();
}

So what you need to do is eliminate all the while loops besides the one in public DocStats()所以你需要做的是消除除了 public DocStats() 中的循环之外的所有 while 循环

in public DocStats you put the while loop Clayton Wilkinson created.在公共 DocStats 中,您放置了 Clayton Wilkinson 创建的 while 循环。

public int getNumberOfCharacters() {

        return characterCount;

    }

    /**
     * 
     * 
     * 
     * @return wordCount
     * 
     */

    public int getNumberOfWords() {

        return wordCount;

    }

    /**
     * 
     * 
     * 
     * @return lineCount
     * 
     */

    public int getNumberOfLines() {

        return lineCount;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM