LordValkyrie
LordValkyrie

Reputation: 75

Java BufferedReader file IO giving odd, inaccurate output

The idea here is that the program goes character by character through a text file, and counts the occurrences of each letter, then stores the amount of occurrences into an array. However, I'm getting odd, inaccurate output I can't seem to fix. Answers online don't seem to be helping. It's probably something very simple that I'm missing, but I need an extra nudge in the right direction.

char token;
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int[] occurences = new int[25];
BufferedReader inFile = new BufferedReader(new FileReader("plaintext.txt"));

while (inFile.read() > -1) {
    token = (char)inFile.read();
    for (int i = 0; i < alphabet.length; i++) {
        if (Character.compare(token, alphabet[i]) == 0) {
            occurences[i] += 1;
        }
    }
}

for (int i = 0; i < occurences.length; i++) {
        System.out.println(occurences[i]);
}

inFile.close();

given that plaintext.txt contains the following:

aaa
aaa
bbb
[];'
abcdefgh qrstuv

I get the following output:

3
1
1
0 
1
0
1
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
0
0

Thanks in advance!

Upvotes: 0

Views: 67

Answers (2)

fvu
fvu

Reputation: 32983

This

while (inFile.read() > -1) {
    token = (char)inFile.read();

translates to: read one char, discard it. Read another, treat it. Read one more, discard, etc.

You can get inspiration here - essentially:

int c;
while ((c = inFile.read()) != -1) {
    // there's no need to declare token before this loop
    char token =  (char) c ;  
}

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

You're ignoring half the chars read in with

while (inFile.read() > -1) {
    token = (char)inFile.read();

Don't do that. Read and use them all

int intToken = 0;
while ((intToken = inFile.read()) > -1) {
    token = (char)intToken;

Upvotes: 1

Related Questions