Hello,
I need to count the amount of letters in a given file. I need to use a array of 26. So using ASCII code to count the letters. A example of what I am trying to achieve would be the following:
Below is the problem I am running into:
The way the code is written below, I get the following output:
Any guidance is much appreciated.
Thanks!
I need to count the amount of letters in a given file. I need to use a array of 26. So using ASCII code to count the letters. A example of what I am trying to achieve would be the following:
File "test.txt" contents: abcdABCDzXy123..! Output: a: 2 b: 2 c: 2 d: 2 x: 1 y: 1 z: 1
Below is the problem I am running into:
The way the code is written below, I get the following output:
Count of each letter found in args[0]. [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea [I@3cecfaea
public static void main (String[] args) throws FileNotFoundException {
int[] count = new int[26]; //This array size leads to problem 2. If array size is '26', problem 1 occurs.
try {
BufferedReader br = new BufferedReader(new FileReader(args [0]));
String s;
while((s = br.readLine()) != null) {
//converts every letter to lowercase
String text2 = s.toLowerCase();
System.out.println("Count of each letter found in " + args[0] + ".");
//Counts occurrence of each letter
for (int i = 0; i < text2.length(); i++) {
char character = text2.charAt(i);
if ((character >= 'a') && (character <= 'z')) {
count[(int)character - 97]++; // The ASCII for 'a' is 97
System.out.println(count);
}
}
}
} catch (FileNotFoundException fne) {
System.out.println("File Not Found :" + fne);
} catch (IOException ioe) {
System.out.println("File Not Found :" + ioe);
}
}
}
Any guidance is much appreciated.
Thanks!