Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Count amount letters that are given in a file

$
0
0
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:

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!

Viewing all articles
Browse latest Browse all 51036

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>