Question: in java complete this program by only changing where it says ( TODO) import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextAnalysis { // The
in java
complete this program by only changing where it says ( TODO)
import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner;
public class TextAnalysis { // The main program is complete and should not be changed public static void main(String[] args)throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); //Priming read System.out.println("Enter the file name or Quit to stop"); String response = keyboard.nextLine(); while(response.equalsIgnoreCase("Quit")==false) { String[] data = readFile(response); analyzeText(data); //Priming read System.out.println("Enter the file name or Quit to stop"); response = keyboard.nextLine(); } System.out.println("That's all folks!"); }
/** Read text data in a file into a String array for processing. The file should * contain the number of lines in the file on the first line. * * @param fileName The name of the file. This file should be stored in the main * project directory. * @return An array containing the contents of the file. * @throws FileNotFoundException If the file is not found in the proper directory. */ public static String[] readFile(String fileName) throws FileNotFoundException { Scanner file = new Scanner(new File ("Sample.txt")); // TODO Read the number of lines
// TODO Construct the array
// TODO Load data into the array // Return the array return null; // the null here is just to satisfy the compiler--you need to return the array } /** Takes a file name and prints out the statistics of how various * punctuation characters are used in the file. This data is intended to * help determine authorship of text. * * @param data An array containing the contents of the file. */ public static void analyzeText(String[] data) { // Set up all the accumulators int commas = 0; int periods = 0; int colons = 0; int semicolons = 0; int exclamations = 0; int questions = 0; int sentences = 1; //TODO 1 is a placeholder--think about what this should be // TODO Step through the file one line at a time, keeping track of which punctuation marks // were found // Count sentences using periods, exclamations and questions
// Show the results System.out.println("There were " + periods/(double)sentences + " periods per sentence."); System.out.println("There were " + questions/(double)sentences + " question marks per sentence."); System.out.println("There were " + exclamations/(double) sentences + " exclamation marks per sentence."); System.out.println("There were " + commas/(double)sentences + " commas per sentence."); System.out.println("There were " + semicolons/(double)sentences + " semicolons per sentence."); System.out.println("There were " + colons/(double) sentences + " colons per sentence."); } /** Count the number of times that the given character occurred on * the given line. * @param line The line of text to be analyzed. * @param punctuation The character sought. * @return The number of occurrences of punctuation in line. */ public static int countOccurrences (String line, char punctuation) { // Set the accumulator count to zero int count = 0; // TODO Step through the line one character at a time, counting the number of // times punctuation occurs
// Return the number of times that the punctuation occurred on the line return count; } }
the printout form the program should read like this
Enter the file name or Quit to stop
Sample.txt
There were 0.6 periods per sentence.
There were 0.2 question marks per sentence.
There were 0.2 exclamation marks per sentence.
There were 0.2 commas per sentence.
There were 0.0 semicolons per sentence.
There were 0.2 colons per sentence.
Enter the file name or Quit to stop
quit
That's all folks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
