0

I am designing a Utility that counts the words and Number of newline characters.

I have done count task but i dont know how to count number of new line charcaters in file.

Code:

System.out.println ("Counting Words"); 

InputStream stream = Run.class.getResourceAsStream("/test.txt");
InputStreamReader r = new InputStreamReader(stream);
BufferedReader br = new BufferedReader (r);     
String line = br.readLine();
int count = 0;

while (line != null) {
    String []parts = line.split(" ");
    for( String w : parts){
        count++;        
    }
    line = br.readLine();
}

System.out.println(count);

test

This is simple file reading by Java Program

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
user1030128
  • 411
  • 9
  • 23

3 Answers3

1

Just look inside the words:

for (char c : w.toCharArray()) {
    if (c == '\n') {
        numNewLineChars++;
    }
}

Which goes inside the for loop you already have.

0
System.out.println ("Counting Words");       
InputStream stream = Run.class.getResourceAsStream("/test.txt");
InputStreamReader r = new InputStreamReader(stream);
BufferedReader br = new BufferedReader (r);     
String line = br.readLine();
int word_count = 0;
int line_count = 0;

while (line != null) {
    String[] parts = line.split(" ");
    word_count += parts.length;
    line_count++;
    line = br.readLine();
}

System.out.println("Word count: " + word_count + " Line count: " + line_count);
Steve P.
  • 14,489
  • 8
  • 42
  • 72
0

It maybe a better option here to use the LineNumberReader class for counting and reading the lines of text. Although this isn't the most efficient way for counting lines in a file (according to this question) it should suffice for most applications.

From LineNumberReader for readLine method:

Read a line of text. Whenever a line terminator is read the current line number is incremented. (A line terminator is usually a newline character '\n' or a carriage return '\r').

This means that when you call the getLineNumber method of the LineNumberReader class, it will return the current line number that has been incremented by the readLine method.

I have included comments in the code below explaining it.

    System.out.println ("Counting ...");       
    InputStream stream = ParseTextFile.class.getResourceAsStream("/test.txt");
    InputStreamReader r = new InputStreamReader(stream);

    /*
     * using a LineNumberReader allows you to get the current 
     * line number once the end of the file has been reached,
     * without having to increment your original 'count' variable.
     */
    LineNumberReader br = new LineNumberReader(r);

    String line = br.readLine();

    // use a long in case you use a large text file
    long wordCount = 0;

    while (line != null) {
        String[] parts = line.split(" ");
        wordCount+= parts.length;
        line = br.readLine();
    }

    /* get the current line number; will be the last line 
     * due to the above loop going to the end of the file.
     */
    int lineCount = br.getLineNumber();

    System.out.println("words: " + wordCount + " lines: " + lineCount);
Community
  • 1
  • 1
Sam
  • 492
  • 2
  • 13