count how frequently each character occurs
I have the code as shown below but i dont know how to count how frequently each character will occur when the user types in their file name. Any help will do
Code :
// Written by A.Person, January 2010
// Answer to Exercises 1 example question 2
class Example1_2
{
private void displayFileContent(String filename)
{
FileInput fileIn = new FileInput(filename);
while (fileIn.hasNextLine())
{
String s = fileIn.nextLine();
System.out.println(s);
System.out.println(s.length());
}
fileIn.close(); // Always close a file after it has been used.
}
private String getFileName()
{
Input in = new Input();
System.out.print("Enter filename: ");
String filename = in.nextLine();
return filename;
}
public void showFile()
{
String filename = getFileName();
displayFileContent(filename);
// Could have written displayFileContent(getFileName());
}
public static void main(String[] args)
{
new Example1_2().showFile();
}
}
Re: count how frequently each character occurs
I really don't quite know what you're asking. Do you have a problem with the code you have provided, or do you want to write new code to count the frequency with which each ASCII character appears? If it is the latter, just read the file one line at a time, and then step through each line one character at a time, and have some mapping from characters to indices in a frequency array whose elements will be incremented whenever the corresponding character is encountered.