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

Description of program: You are to write a program name wordcount.java that prom

ID: 3663684 • Letter: D

Question

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:

1. 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).

2. Count the amount of lines in the file.

3. Count the amount of alphanumeric characters in the file.

4. Count the number of sentences in the file.

5. Count the amount of vowels in the file - only a, e, i, o, u (lower and upper case) are vowels.

6. Count the amount of punctuations in the file.

7. You must output the above information both on the screen as well as an output file name "output.txt".

Here are some requirements for the program:

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.

Implementation:

Use as many generic algorithm as possible sothat the size of the program can be reduced.

Explanation / Answer

//Program:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;


public class Wordcount {

public static void main(String[] args) throws IOException {
System.out.println("Enter the input file name: ");
Scanner scan=new Scanner(System.in);
String filename=scan.nextLine();
  
int lines=countLines(filename);
if(lines==0)
{
System.out.println("File is empty");
}
else
{
File outFile = new File ("output.txt");
FileWriter pWriter = new FileWriter (outFile);
pWriter.write(" Number of lines:"+lines);
int words=countWords(filename);
pWriter.write(" Number of words:"+words);
int sentences=countSentences(filename);
pWriter.write(" Number of Sentences:"+sentences);
int puncts=countPunctuations(filename);
pWriter.write(" Number of numOfPuncts:"+puncts);
//int alphanumeric=countAlphaNumeric(filename,puncts);
//pWriter.println(" Number of lines:"+alphanumeric);
int vowels=countVowels(filename);
pWriter.write(" Number of vowels:"+vowels);
System.out.println("Output is stored in output.txt under same folder");
}

}
private static int countLines(String filename) {
int numOfLines=0;
try {
File file = new File(filename);
try (Scanner input = new Scanner(new FileInputStream(file))) {
while (input.hasNextLine()) {
String line = input.nextLine();
numOfLines++;
}
}
} catch (Exception ex) {
}
return numOfLines;
}
private static int countWords(String filename) {
int numOfWords=0;
try {
File file = new File(filename);
try (Scanner input = new Scanner(new FileInputStream(file))) {
while (input.hasNextLine()) {
String line = input.nextLine();
numOfWords+= new StringTokenizer(line, " ,").countTokens();
}
}
} catch (Exception ex) {
}
return numOfWords;
}

private static int countVowels(String filename) {
int numOfVowels=0;
try {
File file = new File(filename);
try (Scanner input = new Scanner(new FileInputStream(file))) {
while (input.hasNextLine()) {
String line = input.nextLine();
char chars[]=line.toCharArray();
for(int j=0;j<chars.length;j++)
{
if(chars[j]=='a'||chars[j]=='e'||chars[j]=='i'||chars[j]=='o'||chars[j]=='u'||
chars[j]=='A'||chars[j]=='E'||chars[j]=='I'||chars[j]=='O'||chars[j]=='U')
{
numOfVowels++;
}
}
}
}
} catch (Exception ex) {
}
return numOfVowels;
}

private static int countAlphaNumeric(String filename, int puncts) {
int numOfAlphaNumeric=0;
int numOfSpaces=0;
int numOfChars=0;
try {
File file = new File(filename);
try (Scanner input = new Scanner(new FileInputStream(file))) {
while (input.hasNextLine()) {
String line = input.nextLine();
numOfChars+=line.length();
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i)==' ') { // If the delimiters string contains the character
numOfSpaces++;
}
}
}
}
} catch (Exception ex) {
}
numOfAlphaNumeric=numOfChars-numOfSpaces-puncts;
return numOfAlphaNumeric;
}

private static int countPunctuations(String filename) {
int numOfPuncts=0;
String puncts = "?!.,:;";
try {
File file = new File(filename);
try (Scanner input = new Scanner(new FileInputStream(file))) {
while (input.hasNextLine()) {
String line = input.nextLine();
for (int i = 0; i < line.length(); i++) {
if (puncts.indexOf(line.charAt(i)) != -1) { // If the delimiters string contains the character
numOfPuncts++;
}
}
}
}
} catch (Exception ex) {
}
return numOfPuncts;
}

private static int countSentences(String filename) {
int numOfSentences=0;
String delimiters = "?!.";
try {
File file = new File(filename);
try (Scanner input = new Scanner(new FileInputStream(file))) {
while (input.hasNextLine()) {
String line = input.nextLine();
for (int i = 0; i < line.length(); i++) {
if (delimiters.indexOf(line.charAt(i)) != -1) { // If the delimiters string contains the character
numOfSentences++;
}
}
}
}
} catch (Exception ex) {
}
return numOfSentences;
}
  
}