I need help reading in the file at the bottom and counting the punctuation in th
ID: 3806140 • Letter: I
Question
I need help reading in the file at the bottom and counting the punctuation in the file.
Learning Objectives
Use a for loop in a program. (10 points)
Declare, construct, store and retrieve data from a String array (20 points).
Use the two instance methods from the String class (probably length() and charAt(), although there are other possibilities). (10 points)
Write a method that is called multiple times with different arguments. (20 points)
Write a program that uses four methods (including the main method). (10 points)
Read data from a file. (20 points)
10 points will be awarded for the documentation of your program. That means using good names for variables, proper and consistent indentation of code, sufficient comments and meaningful use of whitespace.
When your program is completed and running, have the teaching assistants check it to get credit for the lab. If you do not complete the laboratory during the allotted time, you may submit it on Canvas before Wednesday, March 8 at 11:59 p.m. Only people who attend the whole laboratory will be permitted to submit assignments.
Description
Determining whether newly discovered documents of historical relevance are or are not fraudulent is a complex task. The paper and ink can be analyzed to make sure they are of the proper age and contain ingredients that were in common use in the proper time period. The chemical testing process, however, can damage part of the documents. In addition, forgers can purchase old paper and make ink using historical formulas. Experts can perform comparisons of signatures to see if they are consistent with examples that are known to be legitimate. Most processes that experts use to verify these documents rely on triangulation: seeking to verify the document through as many means as possible.
Computer science can make contributions to this process. Most people who write prolifically, have identifiable habits with their use of language, especially punctuation. Another way to help verify the authenticity of a document would be to compare characteristics of known writing from a person to a newly discovered work to determine if the punctuation characteristics are the same.
Average number of commas per sentence
Average number of semicolons per sentence
Average number of colons per sentence
Average number of exclamation marks per sentence
Average number of question marks per sentence
Average number of periods per sentence
You may assume that all punctuation marks occur at the end of a word and are followed by at least one space, tab, or newline.
If you want to have some fun playing with this program, you can download literature from Project Gutenberg: http://www.gutenberg.org
This project is an introduction to a fascinating area of computer science called text analytics. CS has a faculty member who specializes in this area: Dr. Christan Grant.
Method Signatures
This task can be accomplished using three methods. The first method will read data from a file into a String[].
public static String[] readFile(String fileName)
The file will start with an integer that gives the number of lines of text in the file. The lines of text will follow. A simple example file is below.
5
Line 1 has a period.
Line 2, has a comma and a period.
Line 3: colon and a period.
Line 4 has a question mark?
Line 5 has an exclamation point!
This method will construct a String[] in a local variable that is the correct length, and load the lines of data into the String[] and return it.
The second method and print out the punctuation characteristics of the text stored in the String[]. This method will be called once for each file. The signature of the method is below:
public static void analyzeText(String[] data)
This method should step through the String[] and count the number of times each element of punctuation occurs.
A sample printout from the program is below:
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!
The third method will count the number of times a specific element of punctuation occurred in each line of text. This method can be called with each line of text.
public static int countOccurences (String line, char punctuation)
If String line1 contains: “Line 1 has a period.”, countOccurrences(line1, ‘.’) should return 1 and countOccurrences(line1, ‘?’) should return 0.
Reading Files
To write the first method you will need to use the Scanner class to read from a file. All files we will read are text files and have a .txt (instead of a .java) extension. The files should be put in the home directory of the project. To do this in eclipse, File -> New -> Untitled Text File. Edit the file in the window and save it to a reasonable name in the home directory for your project (not the src directory or the desktop).
Scanners work on files exactly like they do on input from the keyboard with a couple of exceptions.
Java is always convinced that files are trouble, so it insists that all methods that use files announce the fact that there may be problems to the whole world. This is done through a FileNotFoundException in the header of the program. So the first line of the readFile method will have to be:
public static String[] readFile(String fileName) throws FileNotFoundException
The main program will also have to throw a FileNotFoundException.
FileNotFoundException is a class in the Java API. You’ll need to use the API to find the package that contains the class to import.
When files are used with a Scanner, they have to be constructed twice—once to set up the link between the program and the file, and then again to set up the link between the scanner and the program. Files also have to be closed.
// To open a file:
Scanner file = new Scanner (new File (“Sample.txt”));
// To close a file:
file.close();
You’ll need to import another package to use the File class. You can find which one by using the API.
code I have.
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
public class Project7_JosephAskey_ElizabethJantz
{
// 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
{
// TODO Open the file
Scanner file = new Scanner (new File ("Sample.txt"));
// TODO Read the number of lines
int numLine = file.nextInt();
// TODO Construct the array
String[] data;
// TODO Load data into the array
String [] data1 = new String [6];
// Return the array
return data1; // 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 = 5; //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
for (int i = 0; i <= data1.size()-1; i++) {
if (data1.get(i).matches(".")) {
periods += 1;
} else if (data1.get(i).matches(",")) {
commas += 1;
} else if (data1.get(i).matches("?")) {
questions += 1;
} else if (data1.get(i).matches("!")) {
exclamations += 1;
} else if (data1.get(i).matches(";'")){
semicolons += 1;
} else {
System.out.println("Punctuation Marks not 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
data file i need to read in
5
Line 1 has a period.
Line 2, has a comma and a period.
line 3: colon and a period.
line 4 has a question mark?
Line 5 has an exclamation point!
// Return the number of times that the punctuation occurred on the line
return count;
}
}
Explanation / Answer
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
public class Project7_JosephAskey_ElizabethJantz
{
// 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
{
// TODO Open the file
Scanner file = new Scanner (new File ("Sample.txt"));
// TODO Read the number of lines
int numLine = file.nextInt();
// TODO Construct the array
String[] data;
// TODO Load data into the array
String [] data1 = new String [6];
// Return the array
return data1; // 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 = 5; //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
for (int i = 0; i <= data1.size()-1; i++) {
if (data1.get(i).matches(".")) {
periods += 1;
} else if (data1.get(i).matches(",")) {
commas += 1;
} else if (data1.get(i).matches("?")) {
questions += 1;
} else if (data1.get(i).matches("!")) {
exclamations += 1;
} else if (data1.get(i).matches(";'")){
semicolons += 1;
} else {
System.out.println("Punctuation Marks not 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
data file i need to read in
5
Line 1 has a period.
Line 2, has a comma and a period.
line 3: colon and a period.
line 4 has a question mark?
Line 5 has an exclamation point!
// Return the number of times that the punctuation occurred on the line
return count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.