In: Computer Science
JAVA Assignment:
Project File Processing.
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods. When outputting the number of letters that occur in a line, be sure to count upper and lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that do occur in the input line. For example, the input line:-I say HI should produce output similar to the following:-
3 words
1 a
1 h
2 i
1 s
1 y
Please Note!!!!!: in addition to the above, output the result to file named “result.txt”
Program: In this program, we read in a text file, for each line we calculate the number of words in that line, and then for each character of each word we find the frequency of the character. We write the information gathered into an output file. The words in a line are splitted based on different delimiters - commas, periods, spaces, exclamations marks etc.
Below is the implementation:
Code:
import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; class Solution { public static void main(String[] args) throws Exception { // Create a File class instance and pass input file name File file = new File("input.txt"); // Create FileWriter instance and pass result file name FileWriter fileWriter1 = new FileWriter("result.txt"); // Create scanner class instance and pass file class instance Scanner read = new Scanner(file); // Read till the end of line while (read.hasNextLine()) { // Store current line String line = read.nextLine(); // Define regex includes - '!','.',',','_',''','@','?','-' and space // we can add more if we want String regex = "[!._,'-@? ]"; // Create a StringTokenizer object and pass line and regex as parameters StringTokenizer str = new StringTokenizer(line, regex); // Create a list to store words List<String> words = new ArrayList<>(); // Add words to list while (str.hasMoreTokens()) { words.add(str.nextToken()); } // Number of words in current line int numOfWords = words.size(); // Create an array to store frequency of each character int[] freq = new int[26]; // Traverse each word for (String w : words) { // Convert it to lowercase w = w.toLowerCase(); // Update frequency for each char of each word for (int i = 0; i < w.length(); i++) { freq[w.charAt(i) - 'a']++; } } // Write into file fileWriter1.write(numOfWords + " words\n"); for (int i = 0; i < freq.length; i++) { if (freq[i] > 0) { char c = (char) (i + 'a'); fileWriter1.write(freq[i] + " " + c + "\n"); } } } // CLose File and FileWriter instances read.close(); fileWriter1.close(); } }
input.txt:
Output (result.txt):
Note: The input file should be saved in the same folder where the current java class is placed.
#Please ask for any doubts. Thanks.