CSU Banner

CS 150, Fall 2017

Programming Assignment - P9

File Statistics - Gettysburg Address

Due - November 14th, 2017 at 6:00pm

Late - November 15th, 2017 at 8:00am

Objectives of this Assignment

  1. To write a program which performs file input/output,
  2. calculate file statistics on a given text file, and
  3. review some of the Character wrapper class methods.


Create a Java program called P9 with a class named P9, which after created should contain a file named P9.java in the src folder. Please complete the code required from this week's lab first if you have not already done so. All of the Java code you write should be contained in the main method. In addition to determining the number of lines and words in the Gettysburg address file, in P9 you should also determine the following: number of characters, number of uppercase letters, number of lowercase letters, number of digits, number of spaces, and number of special characters.

    Part One
  1. If you still need to import the text file to P9, right on the file here and click either 'Save as' or 'Download link as'. Then save the file to your P9 project directory. Do not save to the src or bin folder.
  2. First, declare and initialize six more integer variables to 0.
  3. Declare and initialize a String to the empty string.
  4. Declare a character variable.
  5. You will now need to add a block of code to determine the rest of the file statistics. This code will go after the nested while loops, but before your code that writes data to the output file.

  6. Part Two
  7. Write a for loop that iterates over all of the elements in the String array.
  8. First, assign the line at the current index to your String object.
  9. Next, write an if statement that breaks the loop if the String is null. Without this check, what exception will be generated?
  10. After the if statement, add the length of the String to your character count variable.
  11. Now, write an inner for loop to determine the type of characters in each String.
  12. First, the inner for loop should assign a single character from the String object to your character variable from step 4. This single character will then be sent through several conditionals.
  13. Following this line of code, write several conditionals that utilize methods of the Character wrapper class. If the character is of a certain type, increment the appropriate integer variable. One type of character you should be counting will be the default case (i.e. for which type of character is there not a wrapper class method?).

  14. Part Three
  15. You are now ready to print six more lines to the output file for this project, Statistics.txt.
  16. First, using your PrintWriter object, print the following to the file:
     Number of Characters: 
    followed by your associated variable.
  17. Next print:
     Number of Uppercase: 
    followed by the appropriate variable.
  18. The next four are as follows, and each print statement should include the variable with the associated statistic.
     Number of Lowercase:
     Number of Digits:
     Number of Spaces:
     Number of Special: 
  19. Make sure the last print statement includes a new line.
  20. If you have not already done so, close your Scanner and PrintWriter objects.

Output File Contents

Number of Lines: 21
Number of Words: 274
Number of Characters: 1475
Number of Uppercase: 13
Number of Lowercase: 1146
Number of Digits: 6
Number of Spaces: 270
Number of Special: 40


Grading Criteria

Submit P9.java to Checkin.

CS Banner
CS Building