簡體   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