In: Computer Science
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
Note: in addition to the above, output the result to file named “result.txt”
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Arrays;
class WordLetterOccurrence {
public static void main(String[] args) {
// Declare an array for storing occurrences of letter a-z in a line
int countArray[] = new int[26];
// Initialize the array with 0's
Arrays.fill(countArray, 0);
try {
// Open file to read
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
String line = "";
// Iterate loop till the end of the file
while ((line = br.readLine()) != null) {
// Split a line using whitespace or commas or period as delimiter
String words[] = line.split("[\\s,.]+");
// Iterate for each word in array
for (int i = 0; i < words.length; i++) {
// Iterate for each letter in each word
for (int j = 0; j < words[i].length(); j++) {
// ch represents current character in lower case
char ch = Character.toLowerCase(words[i].charAt(j));
// index represents ASCII value of character ch
int asciiValue = (int) ch;
// Increment value by 1 at position asciiValue-97 in array
// This is done because
// ASCII value for a-b is 97-122
// So , for ch = a, asciiValue = 97
// So, countArray[asciiValue-97] = countArray[0]
// which represents count of character a at position 0
countArray[asciiValue - 97] += 1;
}
}
// Print number of words in current line
System.out.println(words.length + " words");
// Iterate over countArray
// and check if value at index i is not 0
// (value not 0 , means the character was not present in line)
// If yes, print count and corresponding alphabet
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));
for (int i = 0; i < 26; i++) {
if (countArray[i] != 0) {
bw.write(countArray[i] + " " + (char) (i + 97)+"\n");
System.out.println(countArray[i] + " " + (char) (i + 97));
}
}
bw.close();
System.out.println();
// Reset the array to all 0,
// for keeping count of character of next line
Arrays.fill(countArray, 0);
}
br.close();
} catch (Exception e) {
System.out.println("File doesn't exits!!");
}
}
}