Objectives To gain experience with string objects To gain ex

Objectives: To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files - opening for input and output. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming: Program execution according to the requirements given 60% Naming of program as required 5% Description of program; You are to write a program name wordcount.java that prompt the user for a user input file name, reads the input words and do the following with those words: Count the amount of words in the file. A word can end with any of the following: space (single or multiple), an EOLN character or a punctuation mark (which will be part of the word). Count the amount of lines in the file. Count the amount of alphanumeric characters in the file. Count the number of sentences in the file. Count the amount of vowels in the file - only a, e, i, o, u (lower and upper case) arc vowels. Count the amount of punctuations in the file. You must output the above information both on the screen as well as an output file name \"output.txt\". The program must work even if the input file is empty. If this is the case print a message saying that \"the input file is empty\" and then terminate the program. Use as many generic algorithm as possible so that the size of the program can be reduced.

Solution

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class wordcount {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println(\"Input the File Name to be processed\");
       String file = scanner.nextLine();  
      
       File f = new File(file);
      
       if(f.exists() && !f.isDirectory()) {
       try (BufferedReader br = new BufferedReader(new FileReader(f))) {
           int countLines = 0,countWords = 0,countAlphaNumericChars = 0,countSentences = 0;
           String line;
           String delimiters = \"?!.\";

           while ((line = br.readLine()) != null) {
               countLines++;
               String[] words = line.replaceAll(\"\\\\s+\",\" \").split(\" \");
               countWords += words.length;
               for (int i = 0; i < line.length(); i++) {
                       if (delimiters.indexOf(line.charAt(i)) != -1) { // If the delimiters string contains the character
                   countSentences++;
                    }
                }          
               for(String s:words) {
                   String pattern = \"[a-zA-Z0-9]*\";
                       String newString = s.replaceAll(pattern, \"\");
                       countAlphaNumericChars += (s.length()-newString.length());
               }
           }

           System.out.println(\"Total Lines : \"+countLines);
           System.out.println(\"Total Words : \"+countWords);
           System.out.println(\"Total Alpha Numeric Chars : \"+countAlphaNumericChars);
           System.out.println(\"Total Sentences : \"+countSentences);
       }
       catch (Exception e) {
           e.printStackTrace();
       }
       }
       else {
           System.out.println(\"File Not present in this directory\");
       }


   }
}

 Objectives: To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files - opening for input and output. D
 Objectives: To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files - opening for input and output. D

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site