Program Behavior
Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results.
Here is a sample run of the program. User input is shown in red.
Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of capitalized words: 19
Make sure the output of your program conforms to this sample run. Of course, the values produced will be different depending on the input file being analyzed.
The sample input file that was used for this example can be downloaded from here. You can use it for testing your program, but you should also test it against other input files. To use a file for testing, just add it to the project folder.
The contents of that sample input file are:
This is a sample text file used as input for Project 2. It contains regular English text in sentences and paragraphs that are analyzed by the program. The program counts the number of lines in the input file, as well as the total number of words. It also counts the number of long words in the file. A word is considered long if it has more than five characters. The program also counts the number of sentences by counting the number of periods in the file. Finally, the program counts the number of capitalized words in the input file. It uses a separate method to determine if a word is capitalized. Blank lines will count toward the line count, but do not contribute any words. This program uses Scanner objects in three ways. One Scanner object is used to read the file name that the user types at the keyboard. Another Scanner is used to read each line in the file, and the third is used to parse the individual words on the line. This concludes this sample input file. Have a nice day.
Program Design
Create a new BlueJ project called Project2 and inside that create a class called TextAnalyzer. Add a static method called analyze. which will do most of the process for this program. The header of the method should look like this:
public static void analyze() throws
IOException
Notice the addition of the throws keyword. This simply tells the
compiler that the code inside the method might cause a run-time
error of type IOException. This will happen in the case when you
enter a file name that doesn't exist inside the project's folder.
Using the throws keyword, you are delegating the responsibility of
handling this IO (Input Output) run-time error to the caller of the
method so you don't need to handle an error like this in your
code.
Use three different Scanner objects to accomplish this program. One will read the name of the input file from the user, another will read each line of the input file, and the third will be used to parse each line into separate words.
Determine the appropriate places in your program to count the following:
For the purposes of our analysis, a word is defined as any set of continuous characters separated by white space (spaces, tabs, or new line characters). Punctuation will get caught up in the words. For example, in the sample input file "Finally," is a considered a single word, including the comma. Likewise, "2." and "ways." are words.
The sentence count is really just a count of the periods in the input file. Or, more precisely for your program, it is the count of the number of words that contain a period. Notice the difference between counting lines and counting sentences.
You will define a second, separate method to help analyze capitalized words. Write a method called wordIsCapitalizedthat accepts a String parameter representing a single word and returns a boolean result. Return true if the first character of the word is an uppercase alphabetic character and false otherwise. Fortunately, there is a method called isUpperCase already defined in the Character class of the Java API that can help with this.
Developing the Program
As always: work on your solution in stages. Don't try to get the whole thing written before compiling and testing it. Suggestions:
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class TextAnalyzer {
/**
* @param word a String parameter representing a single
word
* @return true if the first character of the word is
an uppercase alphabetic
* character and false otherwise
*/
public static boolean wordIsCapitalized(String word)
{
if
(Character.isUpperCase(word.charAt(0))) {
return
true;
}
return false;
}
public static void analyze() throws IOException {
// read the name of the input
file from the user
Scanner keyboard = new
Scanner(System.in);
// Get file name from user
System.out.print("Enter file name:
");
String fileName =
keyboard.nextLine();
// Declare variable to
store
int linesCount = 0, wordsCount = 0,
longWordsCount = 0, sentencesCount = 0, capitalizedWordsCount =
0;
// read each line of the input
file
// Create an instance of File for
input file
File inputFile = new
File(fileName);
Scanner lineReader = new
Scanner(inputFile);
while (lineReader.hasNextLine())
{
// Increase the
line count
linesCount++;
String line = lineReader.nextLine();
// to parse
each line into separate words.
Scanner parser =
new Scanner(line);
while
(parser.hasNext()) {
// Increase the word count
wordsCount++;
// Get each splitted data from the Scanner
object
String word = parser.next();
// Verify if it long word
if (word.length() > 5) {
longWordsCount++;
}
// if it has period, increase sentences
count
if (word.contains(".")) {
sentencesCount++;
}
// verify if it is capitalized word
if (wordIsCapitalized(word)) {
capitalizedWordsCount++;
}
}
}
// Print the results
System.out.println("Number of
lines: " + linesCount);
System.out.println("Number of
words: " + wordsCount);
System.out.println("Number of long
words: " + longWordsCount);
System.out.println("Number of
sentences: " + sentencesCount);
System.out.println("Number of
capitalized words: " + capitalizedWordsCount);
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
try {
analyze();
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
}
Refer the following screenshots for code indentation:
Sample Run:
**************************************************************************************
Get Answers For Free
Most questions answered within 1 hours.