import java.io.*; import java.util.*; class CountCharactersFromFile { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("data.txt")); String strLine = ""; String str = ""; while ((strLine = br.readLine()) != null) { str += strLine; } String st = str.replaceAll(" ", "").toLowerCase(); char[] third = st.toCharArray(); System.out.println("Character Total"); for (int counter = 0; counter < third.length; counter++) { char ch = third[counter]; int count = 0; /// n*n complexity its a brute forc approach.. for (int i = 0; i < third.length; i++) { if (ch == third[i]) count++; } /// this for loop is used to avoid repeation boolean b = true; for (int j = counter - 1; j >= 0; j--) { if (ch == third[j]) /// if ch already present set it true b = false; } if (b) { // if not present prveviously then print, else dont print System.out.println(ch + " " + count); } } } }