Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help java coding. Thank you You are to write a program name wordcount.java

ID: 3784268 • Letter: N

Question

Need help java coding. Thank you 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 amount of sentences in the file. Count the amount of vowels in the file-only a, e, i, o, u (lower and upper case) are vowel. 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.

Explanation / Answer

import java.util.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class wordcount{
   public static void main(String[] args) throws FileNotFoundException{
       System.out.println("Enter File name: ");
       Scanner reader = new Scanner(System.in);
       String filename=reader.next();
      
       FileReader in = new FileReader(filename);
   BufferedReader br = new BufferedReader(in);

       int tLines=0;
       int tWords=0;
       int talphaNumric=0;
       int vowels=0;
       int punctuations=0;
       int sentences=0;

   try{
           String line;
           while ((line = br.readLine()) != null) {
           String[] words=line.split(" ");
           tLines++;
           tWords+=words.length;

           Pattern p = Pattern.compile("^[a-zA-Z0-9]*$");
           int n=0;
           while(n<words.length){
               Matcher m = p.matcher(words[n]);
               if(m.find()){
                   talphaNumric++;
               }
               String[] tempStr=words[n].split("");
               Pattern p2 = Pattern.compile("[aeiou]");
               Pattern p3 = Pattern.compile("\p{Punct}");
               int l=0;
               while(l<tempStr.length){
                   Matcher m2 = p2.matcher(tempStr[l]);
                   if(m2.find()){
                       vowels++;
                   }
                   else{
                       Matcher m3 = p3.matcher(tempStr[l]);
                       if(m3.find()){
                           punctuations++;
                           Pattern p4 = Pattern.compile("[.]");
                           Matcher m4 = p4.matcher(tempStr[l]);
                           if(m4.find()){
                               sentences++;
                           }
                       }
                   }
                   l++;
               }
               n++;
           }
           }

           if(tLines<1){
               System.out.println("The file is empty.");
           }
           else{
               System.out.println("Total numbers of lines in the file are: ");
               System.out.println(tLines);
               System.out.println("Total numbers of words in the file are: ");
               System.out.println(tWords);
               System.out.println("Total numbers of alphaNumric words in the file are: ");
               System.out.println(talphaNumric);
               System.out.println("Total numbers of vowels in the file are: ");
               System.out.println(vowels);
               System.out.println("Total numbers of punctuations in the file are: ");
               System.out.println(punctuations);
               System.out.println("Total numbers of sentences in the file are: ");
               System.out.println(sentences);  
           }
          
   }
   catch(IOException e){
       e.printStackTrace();
   }

   try{
       PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
       if(tLines<1){
           writer.println("The file is empty.");
       }
       else{
           writer.println("Total numbers of lines in the file are: ");
           writer.println(tLines);
           writer.println("Total numbers of words in the file are: ");
           writer.println(tWords);
           writer.println("Total numbers of alphaNumric words in the file are: ");
           writer.println(talphaNumric);
           writer.println("Total numbers of vowels in the file are: ");
           writer.println(vowels);
           writer.println("Total numbers of punctuations in the file are: ");
           writer.println(punctuations);
           writer.println("Total numbers of sentences in the file are: ");
           writer.println(sentences);
           }
       writer.close();
       } catch (IOException e){
       // do something
       }

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote