count characters, words and lines in file

2019-01-26 02:13发布

This should count number of lines, words and characters into file.

But it doesn't work. From output it shows only 0.

Code:

public static void main(String[] args) throws IOException {
    int ch;
    boolean prev = true;        
    //counters
    int charsCount = 0;
    int wordsCount = 0;
    int linesCount = 0;

    Scanner in = null;
    File selectedFile = null;
    JFileChooser chooser = new JFileChooser();
    // choose file 
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        selectedFile = chooser.getSelectedFile();
        in = new Scanner(selectedFile);         
    }

    // count the characters of the file till the end
    while(in.hasNext()) {
        ch = in.next().charAt(0);
        if (ch != ' ') ++charsCount;
        if (!prev && ch == ' ') ++wordsCount;
        // don't count if previous char is space
        if (ch == ' ') 
            prev = true;
        else 
            prev = false;

        if (ch == '\n') ++linesCount;
    }

    //display the count of characters, words, and lines
    charsCount -= linesCount * 2;
    wordsCount += linesCount;
    System.out.println("# of chars: " + charsCount);
    System.out.println("# of words: " + wordsCount);
    System.out.println("# of lines: " + linesCount);

    in.close();
}

I can't understand what's going on. Any suggestions?

8条回答
甜甜的少女心
2楼-- · 2019-01-26 02:37

Maybe my code will help you...everything work correct

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class LineWordChar {
    public static void main(String[] args) throws IOException {
        // Convert our text file to string
    String text = new Scanner( new File("way to your file"), "UTF-8" ).useDelimiter("\\A").next();
    BufferedReader bf=new BufferedReader(new FileReader("way to your file"));
    String lines="";
    int linesi=0;
    int words=0;
    int chars=0;
    String s="";
    // while next lines are present in file int linesi will add 1
        while ((lines=bf.readLine())!=null){
        linesi++;}
    // Tokenizer separate our big string "Text" to little string and count them
    StringTokenizer st=new StringTokenizer(text);
     while (st.hasMoreTokens()){
        `enter code here`  s = st.nextToken();
          words++;
    // We take every word during separation and count number of char in this words    
          for (int i = 0; i < s.length(); i++) {
              chars++;}
        }
     System.out.println("Number of lines: "+linesi);
     System.out.println("Number of words: "+words);
     System.out.print("Number of chars: "+chars);
 }
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-26 02:40

You have a couple of issues in here.

First is the test for the end of line is going to cause problems since it usually isn't a single character denoting end of line. Read http://en.wikipedia.org/wiki/End-of-line for more detail on this issue.

The whitespace character between words can be more than just the ASCII 32 (space) value. Consider tabs as one case. You want to check for Character.isWhitespace() more than likely.

You could also solve the end of line issues with two scanners found in How to check the end of line using Scanner?

Here is a quick hack on the code you provided along with input and output.

import java.io.*;
import java.util.Scanner;
import javax.swing.JFileChooser;

public final class TextApp {

public static void main(String[] args) throws IOException {
    //counters
    int charsCount = 0;
    int wordsCount = 0;
    int linesCount = 0;

    Scanner fileScanner = null;
    File selectedFile = null;
    JFileChooser chooser = new JFileChooser();
    // choose file 
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        selectedFile = chooser.getSelectedFile();
        fileScanner = new Scanner(selectedFile);         
    }

    while (fileScanner.hasNextLine()) {
      linesCount++;
      String line = fileScanner.nextLine();
      Scanner lineScanner = new Scanner(line);
      // count the characters of the file till the end
      while(lineScanner.hasNext()) {
        wordsCount++;
        String word = lineScanner.next();
        charsCount += word.length();
      } 

    lineScanner.close();
  }

  //display the count of characters, words, and lines
  System.out.println("# of chars: " + charsCount);
  System.out.println("# of words: " + wordsCount);
  System.out.println("# of lines: " + linesCount);

  fileScanner.close();
 }
}

Here is the test file input:

$ cat ../test.txt 
test text goes here
and here

Here is the output:

$ javac TextApp.java
$ java TextApp 
# of chars: 23
# of words: 6
# of lines: 2
$ wc test.txt 
 2  6 29 test.txt

The difference between character count is due to not counting whitespace characters which appears to be what you were trying to do in the original code.

I hope that helps out.

查看更多
三岁会撩人
4楼-- · 2019-01-26 02:43

Different approach. Using strings to find line,word and character counts:

public static void main(String[] args) throws IOException {
        //counters
        int charsCount = 0;
        int wordsCount = 0;
        int linesCount = 0;

        Scanner in = null;
        File selectedFile = null;
        JFileChooser chooser = new JFileChooser();
        // choose file 
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            selectedFile = chooser.getSelectedFile();
            in = new Scanner(selectedFile);
        }

        while (in.hasNext()) {
            String tmpStr = in.nextLine();
            if (!tmpStr.equalsIgnoreCase("")) {
                String replaceAll = tmpStr.replaceAll("\\s+", "");
                charsCount += replaceAll.length();
                wordsCount += tmpStr.split(" ").length;
            }
            ++linesCount;
        }

        //display the count of characters, words, and lines
        System.out.println("# of chars: " + charsCount);
        System.out.println("# of words: " + wordsCount);
        System.out.println("# of lines: " + linesCount);

        in.close();
    }


Note:
For other encoding styles use new Scanner(new File(selectedFile), "###"); in place of new Scanner(selectedFile);.

### is the Character set to needed. Refer this and wiki

查看更多
闹够了就滚
5楼-- · 2019-01-26 02:49

Use Scanner methods:

int lines = 0;
int words = 0;
int chars = 0;
while(in.hasNextLine()) {
    lines++;
    Scanner lineScanner = new Scanner(in.nextLine());
    lineScanner.useDelimiter(" ");
    while(lineScanner.hasNext()) {
        words++;
        chars += lineScanner.next().length();
    }
}
查看更多
狗以群分
6楼-- · 2019-01-26 02:50

Your code is looking at only the first characters of default tokens (words) in the file.

When you do this ch = in.next().charAt(0), it gets you the first character of a token (word), and the scanner moves forward to the next token (skipping rest of that token).

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-26 02:55
public class WordCount {

    /**
     * @return HashMap a map containing the Character count, Word count and
     *         Sentence count
     * @throws FileNotFoundException 
     *
     */
    public static void main() throws FileNotFoundException {
        lineNumber=2; // as u want
        File f = null;
        ArrayList<Integer> list=new ArrayList<Integer>();

        f = new File("file.txt");
        Scanner sc = new Scanner(f);
        int totalLines=0;
        int totalWords=0;
        int totalChars=0;
        int totalSentences=0;
        while(sc.hasNextLine())
        {
            totalLines++;
            if(totalLines==lineNumber){
                String line = sc.nextLine();
                totalChars += line.length();
                totalWords += new StringTokenizer(line, " ,").countTokens();  //line.split("\\s").length;
                totalSentences += line.split("\\.").length;
                break;
            }
            sc.nextLine();

        }

        list.add(totalChars);
        list.add(totalWords);
        list.add(totalSentences);
        System.out.println(lineNumber+";"+totalWords+";"+totalChars+";"+totalSentences);

    }
}
查看更多
登录 后发表回答