0

Can anyone tell me what's wrong with my code, why am I not getting the correct letter count?

This program reads a text file and count each English letters, A-Z, and a-z, not case sensitive.

Thank you for helping.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Solution {
    private static int 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;
    public static void print(){
        int[] in = {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};
        for (int i = 0; i < in.length; i++){
            System.out.println(in[i]);
        }
    }
    public static void main(String[] args) throws FileNotFoundException{

        File file = new File("t.txt");
        Scanner scan = new Scanner(file);
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            line = line.toLowerCase();
            for (int i = 0; i < line.length(); i++) {
                switch(line.charAt(i)) {
                    case 'a': a++;break;
                    case 'b': b++;break;
                    case 'c': c++;break;
                    case 'd': d++;break;
                    case 'e': e++;break;
                    case 'f': f++;break;
                    case 'g': g++;break;
                    case 'h': h++;break;
                    case 'i': i++;break;
                    case 'j': j++;break;
                    case 'k': k++;break;
                    case 'l': l++;break;
                    case 'm': m++;break;
                    case 'n': n++;break;
                    case 'o': o++;break;
                    case 'p': p++;break;
                    case 'q': q++;break;
                    case 'r': r++;break;
                    case 's': s++;break;
                    case 't': t++;break;
                    case 'u': u++;break;
                    case 'v': v++;break;
                    case 'w': w++;break;
                    case 'x': x++;break;
                    case 'y': y++;break;
                    case 'z': z++;break;
                }
            }
        }
        print();        
    }
}
1
  • @tom: you should definitely have a look at hash tables - it could be implemented much easier :) Commented Nov 30, 2013 at 17:59

2 Answers 2

3

The problem is that when you encounter a i, that will increment the variable of the loop, not the one in the array. So you will skip letters.

Change it with :

for (int counter = 0; counter < line.length(); counter++) {
                switch(line.charAt(counter)) {
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! it was so hard for me to figure out since I take i as granted.
1

your problem is in the use the variable i

in your for loop the index counter is i and i is also a variable which you are using to count the occurrences of alphabet 'i'. use this main method, it will work.

public static void main(String[] args) throws FileNotFoundException{

    File file = new File("t.txt");
    Scanner scan = new Scanner(file);
    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        line = line.toLowerCase();
        for (int index = 0; index < line.length(); index++) {
            switch(line.charAt(index)) {
                case 'a': a++;break;
                case 'b': b++;break;
                case 'c': c++;break;
                case 'd': d++;break;
                case 'e': e++;break;
                case 'f': f++;break;
                case 'g': g++;break;
                case 'h': h++;break;
                case 'i': i++;break;
                case 'j': j++;break;
                case 'k': k++;break;
                case 'l': l++;break;
                case 'm': m++;break;
                case 'n': n++;break;
                case 'o': o++;break;
                case 'p': p++;break;
                case 'q': q++;break;
                case 'r': r++;break;
                case 's': s++;break;
                case 't': t++;break;
                case 'u': u++;break;
                case 'v': v++;break;
                case 'w': w++;break;
                case 'x': x++;break;
                case 'y': y++;break;
                case 'z': z++;break;
            }
        }
    }
    print();        
}

also dont forget to close the scanner after you are done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.