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
Note: in addition to the above, output the result to file named “result.txt”
JAVA PROGRAM
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
public class WordLetterCount {
public static void main(String[] args) {
// Array of occurence counts
int countArray[] = new int[26];
// Initializing the array with 0's
Arrays.fill(countArray, 0);
try {
// Opening file to read
BufferedReader br = new BufferedReader(new FileReader("testFile.txt"));
String line = "";
FileWriter fw=new FileWriter("results.txt");
// Iterating loop till the end of the file
while((line = br.readLine())!=null) {
// Splitting lines using whitespace/commas/period as delimiter
String words[] = line.split("[\\s,.]+");
// Iterating for each word in the array
for(int i=0;i<words.length;i++) {
// Iterating for each letter in each word
for(int j = 0;j<words[i].length();j++) {
// ch : current character in lower case
char ch = Character.toLowerCase(words[i].charAt(j));
// index : ASCII value of character ch
int asciiValue = (int)ch;
//Incrementing occurence count
countArray[asciiValue-97]+=1;
}
}
fw.write(words.length+ " words\n");
// 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, means the character was not present in line
// If yes, print count and corresponding alphabet
for(int i =0 ;i<26;i++) {
if(countArray[i]!=0) {
System.out.println(countArray[i] +" "+(char)(i+97));
fw.write(countArray[i] +" "+(char)(i+97)+"\n");
}
}
System.out.println();
fw.write("\n");
// Reset the array to all 0,
// for keeping count of character of next line
Arrays.fill(countArray, 0);
}
br.close();
fw.close();
} catch(Exception e) {
System.out.println("File doesn't exits!!");
}
}
}
PROGRAM SCREENSHOT
INPUT FILE: testFile.txt
OUTPUT FILE : results.txt
Note:
# The alphabet occurance is counted using ASCII values. An array of size 26 is made corresponding to each alphabet. Now
asciiValue-97 in array corresponds to the exact alphabet in the count 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
Get Answers For Free
Most questions answered within 1 hours.