Question

java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text...

java programming

Problem 2 (25 points)
Counting Character Frequency. 

Write a program to analyze a text file (a novel or a report, or just a sequence of letters or symbols), 
by reading the file into a byte array, convert to a string, and then scan the string letter by letter to count the frequencies of all unique characters in the text, after that, the letters with frequencies greater than 0 and the frequencies are reported side by side. 
All members of the class can be static. 


For example if the text file test1.txt contains AAACMJKYYKNa1C 

Your output should look like 

Ascii code: the char: frequency

49: 1: 1

65: A: 3

67: C: 2

74: J: 1

75: K: 2

77: M: 1

78: N: 1

89: Y: 2

97: a: 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class Main {
public static void bubbleSort(char arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[i]
char temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
  
public static void main(String a[]) {
String file_name = "test1.txt"; // Enter your file path
InputStream fileReader = null;
HashMap<Character, Integer> countLetters = new HashMap<Character, Integer>(); //Hashmap to store Character and count of Character

String filedata = ""; // to store byteArray to String
try {
fileReader = new FileInputStream(file_name);
byte file_content[] = new byte[2 * 1024]; //initializing byte array
int read_count = 0;
while ((read_count = fileReader.read(file_content)) > 0) {
filedata = new String(file_content, 0, read_count); //converting byte array to string
}

char[] countOccurrence = filedata.toCharArray(); // converting string to char array to count letters Occurrences
int[] a1=new int[127];
for(int i=0;i<127;i++)
{
a1[i]=0;
}
for(int i=0;i<countOccurrence.length;i++)
{
a1[(int)countOccurrence[i]]++;
}
for(int i=0;i<127;i++)
{
if(a1[i]!=0)
System.out.println(i+" : "+(char)i+" : "+a1[i]);
}
  
} catch (FileNotFoundException e) {//handling file not found exception
e.printStackTrace();
} catch (IOException e) {// handling io exception
e.printStackTrace();
} finally {
try {
if (fileReader != null)
fileReader.close();//closing FileInputStream
} catch (Exception ex) {

}
}
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need eclipse code for : Write a program that analyzes text written in the console...

    I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...

  • c# csci312: character counter - vector design a program that reads in an ascii text file...

    c# csci312: character counter - vector design a program that reads in an ascii text file (provided) ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provide... C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII...

  • Counting characters, words, and lines based on a text file

    I did the assigment but inside mainWrite a C program (called counting.c) that counts the number of characters, words and lines readfrom standard input (stdin) until EOF is reached. This means counting.c must contain a mainfunction along with other function.- Assume the input is ASCII text of any length.-Every byte read from stdin counts as a character except EOF.- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has...

  • JAVA Write an application to test the HuffmanTree class. Your application will need to read a...

    JAVA Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file,...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • For this problem, you will write a program to form a frequency table of the letters...

    For this problem, you will write a program to form a frequency table of the letters in a text file, that is, how many times each letter appears in the file In Python 3.7. Such a frequency table might be useful for compressing a text file. Because different letters appear with different frequencies, we can compress a file by using shorter codes for common letters and longer codes for letters that appear less frequently. Dictionaries provide an elegant way to...

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

  • Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array...

    Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array listing words and their counts in sorted order Function Description: Have you ever seen those fancy word-clouds based on the frequency of word occurrences in some text? Well the first step of making a graphic like that is to figure out how often the words in some text occur, which is exactly what this function will do. You will count the number of occurrences...

  • use  JOptionPane to display output Can someone please help write a program in Java using loops, not...

    use  JOptionPane to display output Can someone please help write a program in Java using loops, not HashMap Test 1 Grades After the class complete the first exam, I saved all of the grades in a text file called test1.txt. Your job is to do some analysis on the grades for me. Input: There will not be any input for this program. This is called a batch program because there will be no user interaction other than to run the program....

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT