Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Class called FileCharCount that provides char counts for any text file

ID: 3855697 • Letter: C

Question

Create a Class called FileCharCount that provides char counts for any text file (input file used as constructor parameter).

Character == ASCII char's that exist in a text file

Integer == count of each char in that data file

At issue is the fact that TreeMap<Character, Integer> would be sorted by Character, but we want this sorted by the Integer count of each Character. And you cannot just flip that to TreeMap<Integer, Character> because identical Integer counts will omit the first occurrence!!!

SUBMIT:

FileCharCount.java

CharInt.java

<><><><> MAIN <><><><>

import java.io.File;

import java.util.*;

public class CharMain {

   public static void main(String[] args) {

       //MAIN TESTING CENTER

       Scanner data = new Scanner(new File("moby.txt"));

       FileCharCount working = new FileCharCount(data); // reads data from file, all the code to read file

       System.out.println(working.getCounts(3)); // returns highest 3 counts e.g. =199250, e=115002, t=86488...

       // I counted 199250 space characters in moby.txt file, yes 'e' is the most common char as Wikipedia predicts.

       System.out.println(working.getCounts('a')); // returns int count of char 'a' (zero if not present)

System.out.println(working.getCounts(-3)); // returns a Collection of lowest 3 counts, e.g. $=2, [=2, ]=2

       System.out.println(working.getCounts()); // returns the ENTIRE Collection like a Map (like e=9, etc.) crazy long...

   }

}

Explanation / Answer

Code:

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.util.Scanner;

public class Main {

public static void main (String[] args){

boolean tryagain;

Scanner input = new Scanner(System.in);

System.out.println("Please Enter a file name:");

String filename = input.next();

  

FileReader progFileReader= null;

BufferedReader progBufferReader= null;

int[] upper = new int[26];

int[] lower = new int[26];

char charNow;

  

try {

progFileReader= new FileReader(filename);

progBufferReader= new BufferedReader(progFileReader);

String line;

while((line = progBufferReader.readLine()) != null){

for (int ch = 0; ch < line.length(); ch++){

charNow = line.charAt(ch);

if (charNow >= 'A' && charNow <= 'Z'){

upper[charNow-'A']++;

}else{

if (charNow >= 'a' && charNow <= 'z')

lower[charNow-'a']++;

}

}

}

  

progBufferReader.close();//closing the file

}

catch (Exception e){

System.out.println(e.getMessage()); //printing the error message

}

for (int letter=0; letter < upper.length; letter++){

System.out.print((char)(letter+'A')+":"+upper[letter]);

System.out.print(" "+(char)(letter+'a')+":"+lower[letter]);

System.out.println();

}

}

}

Output: