***Problem****In this program if the frequency is large (say, 9) the frequency shows it as 9.000000000006.
import java.io.*; public class Freq { public static void main(String[] args) { System.out.println("Type string : "); String str = ""; InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); try { str = reader.readLine(); } catch (Exception e) { } System.out.println("String is : " + str);//part 2 string input /*finding frequency of characters*/ double len = str.length(); double count = 0; double prob = 0; double ref = 1000; double freq = 0; double prqueue[] = new double[96]; char charArr[] = new char[96]; double temp = 0; char flag; for (int i = 0; i < 96; i++) { char currChar = (char) (32 + i); for (int k = 0; k < len; k++) { char currStrChar = str.charAt(k); if (currChar == currStrChar) { count = count + (1 / ref);//this is used if the file is too large } } freq = count * ref; prob = freq / len; if (freq != 0) { System.out.println("occurence of " + currChar + ": " + freq + " and probability is : " + prob); } prqueue[i] = prob; charArr[i] = currChar; count = 0; } System.out.println(); System.out.println(); System.out.println("PRIORITY QUEUE :"); /*priority queue*/ for (int x = 0; x < 95; x++) { for (int y = 0; y < 95 - x - 1; y++) { if (prqueue[y] > prqueue[y + 1]) { temp = prqueue[y]; flag = charArr[y]; prqueue[y] = prqueue[y + 1]; charArr[y] = charArr[y + 1]; prqueue[y + 1] = temp; charArr[y + 1] = flag; } } } for (int x = 0; x < 95; x++) { if (prqueue[x] != 0) { System.out.println(charArr[x] + " has value in priority queue " + prqueue[x] + ", "); } } } }