I am new to java and can not figure out why my code does not print out both the text file and the letter occurrence

JavaLearner :

My code prints only the first line of the file and the linecount is 1. However the array prints the correct amount of the letters. How can I change my code to print both text and letter occurrence. Also i want to print in the 27th position of the array non alphabetic character such as symbol and spaces.Thanks in advance.

 import java.io.*;
 public class Test { 
 public static void main(String[] args)throws Exception {

     int lineCount=0; // Variale used to count the numbers of line. 
     int nextChar;
     char c;

     File file = new File("file.txt");  //  file in same folder

     BufferedReader readFile = new BufferedReader(new FileReader(file)); 
     String lines= " "; 
     int[] count = new int[27];
     char ch;

     while ((lines = readFile.readLine()) != null){

        System.out.println(lines);
        while ((nextChar = readFile.read()) != -1) {
        ch = ((char) nextChar);

            // Does this code count even uppercase or shall i  
            // convert it the text to lower case.

            if (ch >= 'a' && ch <= 'z'){
                count[ch - 'a']++;

            }
        }
        lineCount++;
    }

    System.out.println("file.txt containes " + lineCount + " lines.");

    for (int i = 0; i < 26; i++) {
        System.out.printf("%c %d", i + 'A', count[i]);
    }        
} 
} 
jste89 :

You were very close with your original answer!

The main problem was the nested while loop which was reading to the end of the file - that's why the first line was printed and the counts were correct but the other lines were not printed. The reason for that is BufferedReader is backed by a buffer (as the name suggests). The first call to readLine returned a String containing all the characters in the buffer up to the first newline character. Each call to the read method in the following while loop then moved the position of the buffer along by one character until it reached the end of the file, at which point the read method returns -1 and the loop is exited. By the time the second call to readLine is made the buffer is already at the end position so a null is returned.

You can solve the problem by iterating over the bytes within the line returned from the call to the readLine method.

Here's a working example:

public static void main(String[] args) throws Exception {
    int lineCount = 0;// Variale used to count the numbers of line.
    File file = new File("file.txt");  //  file in same folder

    BufferedReader readFile = new BufferedReader(new FileReader(file));
    String lines;
    int[] count = new int[27];
    char ch;

    while ((lines = readFile.readLine()) != null) {
        System.out.println(lines);
        for (byte charByte : lines.getBytes()) {
            ch = (char) charByte;

            // Does this code count even uppercase or shall i convert
            // it the text to lower case.

            if (ch >= 'a' && ch <= 'z') {
                count[ch - 'a']++;
            // Count non-alpha characters here. Node: this will count numeric values also...
            } else if (ch < 'A' || ch > 'Z') {
                count[26]++;
            }
        }
        lineCount++;
    }

    System.out.println("file.txt containes " + lineCount + " lines.");

    for (int i = 0; i < 26; i++) {
        System.out.printf("%c: %d\n", i + 'A', count[i]);
    }
    System.out.printf("Special characters: %d\n", count[26]);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=107607&siteId=1