Question

In: Computer Science

Implement the following method take takes the name of an ASCII text file and displays the...

Implement the following method take takes the name of an ASCII text file and displays the frequency of each character in the file. (6 pts)

public static void CountCharacters(String filename)

{

...

}

// Main provided for testing

public static void main(String args[])

{

CountCharacters("testfile.dat");

}

Output should be in the following format:

ASCII CODE - COUNTS

10 - 1

66 - 2

104 - 1

Solutions

Expert Solution

import java.nio.file.*;
import java.util.*;
public class Ans {
  
public static void CountCharacters(String filename)
{
System.out.println("ASCII CODE - COUNTS");
String data = "";
// Read file into String
try {
data = new String(Files.readAllBytes(Paths.get(filename)));
}catch(Exception e) {
   e.printStackTrace();
}
// Map to store frequency counts
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0;i<data.length();i++) {
   int k = data.charAt(i);
   // Map already contains key
   if(map.containsKey(k)) {
       int val = map.get(k);
       map.put(k,val+1);
   }
   // New key in map
   else
       map.put(k, 1);
}
// Display keys in sorted order
TreeMap<Integer, Integer> sorted = new TreeMap<>();
sorted.putAll(map);
for (Map.Entry<Integer, Integer> entry : sorted.entrySet())
   System.out.println(entry.getKey()+"\t"+entry.getValue());
  
}
public static void main(String args[])
{
   CountCharacters("testfile.dat");
}
}


Related Solutions

4.31 Implement function duplicate() that takes as input the name (a string) of a file in...
4.31 Implement function duplicate() that takes as input the name (a string) of a file in the current directory and returns True if the file contains duplicate words and False otherwise. duplicate('Duplicates.txt') True duplicate('noDuplicates.txt') False Please solve using Python Language and without using str.maketrans please. Just simple programming, Thank youuuuu!!!!!
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name &quot;Test&quot; and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named &quot;361&quot; 5-Copy file &quot;Test&quot; to...
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
Write a method ( C++ ) map occurance_map(const string path); that reads in an ascii text...
Write a method ( C++ ) map occurance_map(const string path); that reads in an ascii text file and returns an assocation where each key is a word in the text file and each value is the number of occurances of that word. Ignore punctuation and numbers. The method should be case-insensitive and should store the keys as lowercase. A word is definited by an string consisting entirely of alpha-numeric characters or apostrophes (single quote characteris). For example, if the file...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER IS PREFERRED THANK YOU IN ADVANCE...
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Implement the addSecond method in IntSinglyLinkedList. This method takes an Integer as an argument and adds...
Implement the addSecond method in IntSinglyLinkedList. This method takes an Integer as an argument and adds it as the second element in the list. Here is an example of adding the Integer 7 to a list with two elements. Abstract view: addSecond(7) on the list [12, 100] turns the list into [12, 7, 100] Implement the rotateLeft method in IntSinglyLinkedList. It moves all elements closer to the front of the list by one space, moving the front element to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT