0

I have an assignment where I have to find the letter frequency in a given text from user with GUI in Java. The problem is that I am Albanian and our alphabet has 36 letters: a,b,c...etc but also 'dh' 'll' 'nj' 'th' 'xh' 'zh' 'ë' 'Ç'..

should I use 'if' to compare the next letter: if I have 'd' and the next one is 'h' I consider it as one letter 'dh' and find its frequency? Is there any other way in Java? What about the two last letters? Does Java recognise them as characters? Thanks.

rpax
  • 4,468
  • 7
  • 33
  • 57
Angie
  • 21
  • 6

1 Answers1

1

You will need to use Unicode-aware methods such as Character.isLetter(). Here is the code from http://rosettacode.org/wiki/Letter_frequency#Java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class LetterFreq {
    public static int[] countLetters(String filename) throws IOException{
        int[] freqs = new int[26];
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String line;
        while((line = in.readLine()) != null){
            line = line.toUpperCase();
            for(char ch:line.toCharArray()){
                if(Character.isLetter(ch)){
                    freqs[ch - 'A']++;
                }
            }
        }
        in.close();
        return freqs;
    }

    public static void main(String[] args) throws IOException{
        System.out.println(Arrays.toString(countLetters("filename.txt")));
    }
}

Here I modified it to include Unicode characters

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class LetterFreq {
    public static Map<Character, Integer> countLetters(String filename) throws IOException{
        Map<Character, Integer> freqs = new HashMap<Character, Integer>();
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String line;
        Integer f;
        while((line = in.readLine()) != null){
            line = line.toUpperCase();
            for(Character ch:line.toCharArray()){
                if(Character.isLetter(ch)){
                    f = freqs.get(ch);
                    if (f == null) {
                        freqs.put(ch, 0);   
                    }
                    else {
                        freqs.put(ch, ++f);
                    }
                }
            }
        }
        in.close();
        return freqs;
    }

    public static void main(String[] args) throws IOException{
        Map<Character, Integer> freqs = countLetters("filename.txt");
        Set<Character> keySet = freqs.keySet();
        for (Character key : keySet) {
                    System.out.println("Char: " + key + "  Count: " + freqs.get(key));
        }

    }
}
user2314737
  • 27,088
  • 20
  • 102
  • 114