0

So I have pretty much completed (I think) my wc program in Java, that takes a filename from a user input (even multiple), and counts the lines, words, bytes (number of characters) from the file. There were 2 files provided for testing purposes, and they are in a .dat format, being readable from dos/linux command lines. Everything is working properly except for the count when there are \n or \r\n characters at the end of line. It will not count these. Please help?

import java.io.*;
import java.util.regex.Pattern;

public class Prog03 {


private static int totalWords = 0, currentWords = 0;
private static int totalLines =0, currentLines = 0;
private static int totalBytes = 0, currentBytes = 0;

public static void main(String[] args) {


    System.out.println("This program determines the quantity of lines,                                    words, and bytes\n" +
"in a file or files that you specify.\n" +
"\nPlease enter one or more file names, comma-separated: ");

        getFileName();

         System.out.println();

} // End of main method.


public static void countSingle (String fileName, BufferedReader in) {

    try {

        String line;

        String[] words;

        //int totalWords = 0;

        int totalWords1 = 0;

        int lines = 0;

        int chars = 0;

        while ((line = in.readLine()) != null) {

            lines++;
            currentLines = lines;

            chars += line.length();
            currentBytes = chars;

            words = line.split(" ");

            totalWords1 += countWords(line);
            currentWords = totalWords1;

        } // End of while loop.

        System.out.println(currentLines + "\t\t" + currentWords + "\t\t" +     currentBytes + "\t\t"
                + fileName);

    } catch (Exception ex) {

        ex.printStackTrace();

    }
}

public static void countMultiple(String fileName, BufferedReader in) {

    try {

        String line;

        String[] words;

        int totalWords1 = 0;

        int lines = 0;

        int chars = 0;

        while ((line = in.readLine()) != null) {

            lines++;
            currentLines = lines;

            chars += line.length();
            currentBytes = chars;

            words = line.split(" ");

            totalWords1 += countWords(line);
            currentWords = totalWords1;


        } // End of while loop.
        totalLines += currentLines;            
        totalBytes += currentBytes;
        totalWords += totalWords1;

    } catch (Exception ex) {

        ex.printStackTrace();

    }

} // End of method count().

private static long countWords(String line) {

    long numWords = 0;

    int index = 0;

    boolean prevWhitespace = true;

    while (index < line.length()) {

        char c = line.charAt(index++);                

        boolean currWhitespace = Character.isWhitespace(c);

        if (prevWhitespace && !currWhitespace) {

            numWords++;

        }

        prevWhitespace = currWhitespace;

    }

    return numWords;

} // End of method countWords().

private static void getFileName() {        

    BufferedReader in ;        

      try {

    in = new BufferedReader(new InputStreamReader(System.in));

        String fileName = in.readLine();

        String [] files = fileName.split(", ");

          System.out.println("Lines\t\tWords\t\tBytes" + 
                "\n--------\t--------\t--------");

        for (int i = 0; i < files.length; i++) {
        FileReader fileReader = new FileReader(files[i]);

        in = new BufferedReader(fileReader);

        if (files.length == 1) {
            countSingle(files[0], in);
            in.close();
        }
        else {
        countMultiple(files[i], in);  
            System.out.println(currentLines + "\t\t" + 
                    currentWords + "\t\t" + currentBytes + "\t\t"
                + files[i]);
        in.close();
        }
        }    
        if (files.length > 1) {
          System.out.println("----------------------------------------" + 
                "\n" + totalLines + "\t\t" + totalWords + "\t\t" +     totalBytes + "\t\tTotals");
        }
      }

      catch (FileNotFoundException ioe) {
        System.out.println("The specified file was not found. Please recheck   "
                + "the spelling and try again.");

        ioe.printStackTrace();

    } 

       catch (IOException ioe) {

                ioe.printStackTrace();

            }


}
} // End of class

that is the entire program, if anyone helping should need to see anything, however this is where I count the length of each string in a line (and I assumed that the eol characters would be part of this count, but they aren't.)

  public static void countMultiple(String fileName, BufferedReader in) {

    try {

        String line;

        String[] words;

        int totalWords1 = 0;

        int lines = 0;

        int chars = 0;

        while ((line = in.readLine()) != null) {

            lines++;
            currentLines = lines;

            **chars += line.length();**
            currentBytes = chars;

            words = line.split(" ");

            totalWords1 += countWords(line);
            currentWords = totalWords1;


        } // End of while loop.
        totalLines += currentLines;            
        totalBytes += currentBytes;
        totalWords += totalWords1;

    } catch (Exception ex) {

        ex.printStackTrace();

    }

}
Camdeazy
  • 23
  • 4

2 Answers2

0

BufferedReader always ignores new line or line break character. There is no way to do this using readLine().

You can use read() method instead. But in that case you have to read each character individually.

Rahman
  • 3,755
  • 3
  • 26
  • 43
  • Could I do something like `chars += in.read(); if (in.read() == ' ' chars-=1`? – Camdeazy Sep 24 '15 at 09:58
  • you cant do `chars += in.read()` , because in.read() returns the ascii code of the read character. So what you can do is , put a check for line feed(as you said in your comment) like : `if (in.read() == 10) chars-=1`; where 10 is the ascii code of line feed. – Rahman Sep 24 '15 at 10:35
0

just a comment, to split a line to words, it is not enough to split based on single space: line.split(" "); you will miss if there are multiple spaces or tabs between words. better to do split on any whitespace char line.split("\\s+");

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • Thanks for that bit of info, didn't know that – Camdeazy Sep 24 '15 at 09:51
  • I tried using this, but it still only reads it as one byte. I put a tab in there and the count only increased by 1, what am I doing wrong? – Camdeazy Sep 24 '15 at 10:25
  • it still reads what as one byte? you put a tab where? what isn't working? please elaborate with examples ... – Sharon Ben Asher Sep 24 '15 at 11:26
  • Tried using line.split("\\s+") instead of just line.split(" "). I added a tab to the beginning and in the middle of a sample.txt file I created for testing purposes. – Camdeazy Sep 24 '15 at 17:06
  • you do not count words with the split command. actually, you do nothing with the `String[]` array that is the output of the split. You use your own written `countWords()` method that is doing something completely different – Sharon Ben Asher Oct 08 '15 at 06:57