简体   繁体   中英

Need to count characters in text file

i have to write code that reads a text file and tells me how many lines and characters are in the file. I had it working but then i realized i had to ignore whitespace gaps so i wrote a method to do it. It works fine for one line but if i have more than one line it seems to count any whitespace. Any help would be appreciated

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Inputfile {

public static void main(String[] args) {
    System.out.println("file name:");
    Scanner sc = new Scanner(System.in);
    String fn = sc.next();

    int nrl = 0, nChar = 0;// nrl for number of lines
    String line;// get line content variable

    try {
        File fs = new File("C:/" + fn);
        nChar = length_ws(fs);
        FileReader fr;// for reading file

        fr = new FileReader(fs);
        LineNumberReader lnr = new LineNumberReader(fr);
        while (lnr.readLine() != null) {
            nrl++;// count number of lines
        }
        JOptionPane.showMessageDialog(null, "number of lines:" + nrl + "\ntotal number of chars:" + nChar);

        lnr.close();
        fr.close();// close file
    } catch (FileNotFoundException ex) {
        System.err.println("File not found");
        System.exit(0);
    } catch (IOException ex) {

    }
}

public static int length_ws(File f) throws IOException {
    FileReader fr = null;
    fr = new FileReader(f);
    int i;
    i = 0;
    int c = 0;
    do {

        c = fr.read();// read character

        if (c!= ' ') // count character except white space
            i++;
    } while (c != -1);
    return i - 1;// because the is counted even the end of file
}
}

I don't think it is reading the space but the line feed (since these are char to).

I suggest that you do only read the file once (now it seems that you read it twice).

As char arrives

  c = fr.read()

you evalute which char it is check out the asci table ASCII TABLE , you have space,tabs and line feeds (watch out depending on format you can have two chars LF and CR for line feed)

If you have valid char you advance your char counter. If you have valid char for linefeed you advance your line count.

Hope this help and improves your coding, good luck

Seeing your comment I added this code, its not perfect but a start

int LF = 10; // Line feed
    int CR = 13; // Chr retrun
    int SPACE = 32;// Space
    int TAB = 9; // Tab

     FileReader fr = null;
    int numberOfChars = 0;
    int numberOfLines = 0;
    int c = 0;
    try {
        do {

            fr = new FileReader(new File("fileName.txt"));
            c = fr.read();// read character
            if (c > SPACE) { // space (ignoring also other chars 
                numberOfChars++;
            }
            if (c == LF) { // if does not have LF try CR
                numberOfLines++;
            }

        } while (c != -1);

    } catch (Exception e) {
        e.printStackTrace();
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e1) {
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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