繁体   English   中英

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

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

我正在尝试编写一个程序,以类似于文字处理器的方式收集文本文件的统​​计信息,它会告诉您您编写了多少个字符、单词和行。

该程序的目的是向用户询问文件名(使用扫描仪)并输出统计信息。

这是我到目前为止所拥有的:

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;
}
}

在 main 方法中测试我的类后,我没有得到正确的输出。 下面是主要方法:

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
}
}

谁能指出为什么我的代码不起作用并建议任何替代方法来修复它?

您正在 getNumberOfCharacters() 方法中扫描文件,这就是它非零的原因。 其他为 0,因为扫描仪位于文件末尾。

您需要将 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();
}

所以你需要做的是消除除了 public DocStats() 中的循环之外的所有 while 循环

在公共 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