Question

In: Computer Science

Write a program that will read in from input file one line at a time until...

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”

Solutions

Expert Solution

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!!");
    }
  }
}


Related Solutions

JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
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...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
Write a batch program that takes and echoes input data one character at a time until...
Write a batch program that takes and echoes input data one character at a time until EOF is encountered and then prints a summary such as The 14 lines of text processed contained 20 capital letters, 607 lowercase letters, and 32 punctuation marks. Program: C
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT