I have trouble reading the char
part of a text file and making each char
have a value that is the number of that char
in the file.
For example:
i'm eating
should be:
i = 2
m = 1
e = 1
a = 1
t = 1
n = 1
g = 1
Can anyone help me?
I have trouble reading the char
part of a text file and making each char
have a value that is the number of that char
in the file.
For example:
i'm eating
should be:
i = 2
m = 1
e = 1
a = 1
t = 1
n = 1
g = 1
Can anyone help me?
Java char type is a 16-bit integer (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html), so a relatively small array can store the counters:
int statistics[]=new int[65536];
int onechar;
while(-1!=(onechar=br.read())){
statistics[onechar]++;
}
for(int i=' ';i<statistics.length;i++){
if(statistics[i]>0){
System.out.println(String.format("%c: %d",i,statistics[i]));
}
}
Where br
is the BufferedReader