CSU Banner

CS 150, Spring 2018

Programming Assignment - P8

File Statistics - Gettysburg Address

Due - May 1, 2018 at 6:00pm

Late - May 2, 2018 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.

Instructions

Create a Java program called P8 with a class named P8, which after created should contain a file named P8.java in the src folder. 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 P8 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.

Lab 14 You will complete parts 1-3 during lab.

    Part One
  1. In order to import the text file to P8, right on the file here and click either 'Save as' or 'Download link as'. Then save the file to your P8 project directory. Do not save to the src or bin folder. After saving to your P8, right click on the P8 project and click refresh. Once P8 has been refreshed you should see Gettysburg.txt appear in the project directory.
  2. First, copy the following code into the body of your main method:
      try {
          // Declare and initialize variables.
    
          // Read lines in file into String array and count words per line.
    
          // Write file statistics to Statistics.txt.
    
          // Close Scanner and PrintWriter.
    
      } catch (Exception e) {
    	System.out.println("Error");
    	e.printStackTrace();
      } // end try-catch
      
    All of the remaining code below should reside in the try-catch block. You do not need to make any changes to the try-catch code.
  3. Declare and initialize two File objects, one with the source as args[0], and the other as args[1].
  4. Declare and initialize a Scanner object to read from the file object related to args[0].
  5. Declare and initialize a PrintWriter object to write to the file object pertaining to args[1].
  6. Declare and initialize a String array to a size of 25 elements.
  7. Declare and initialize eight integer variables to 0. These variables will represent statistics about the file, such as line count, character count, etc.
  8. Declare and initialize a String to the empty string.
  9. Declare a character variable.
  10. Set up the program arguments for P9 to read Gettysburg.txt Statistics.txt - make sure there is a space in between file names.

  11. Part Two
  12. Write a while loop that continues to iterate as long as the file has another line.
  13. Inside the loop, using your Scanner, assign the first line to the first element of the String array. As the loop iterates, lines should be assigned to sequential elements of the array; so do not hard code index values.
  14. You should use one of your integer variables as the index variable for the String array. This same variable will also act, later on, as the variable to use when printing the number of lines in the file.
  15. Next, create another Scanner object (still inside the while loop). The purpose of this Scanner will be to read each token of the lines previously read by the other Scanner object above.
  16. The input to this second Scanner is the line of text just assigned to the String array. You should access the line via the array, not by writing another line of code with the first Scanner object.
  17. Now, write an inner while loop that continues to iterate as long as the second Scanner object has more tokens.
  18. Inside this while loop, do two things: (1) call the next method on the current Scanner, but do not assign the token to anything; and (2) increment your second integer variable, which will represent the number of words in the file.
  19. Before exiting the outer while loop, but outside of the inner while loop, increment the integer representing number of lines in the file.

  20. Part Three
  21. You are now ready to print a few lines to the output file for this project, Statistics.txt. After running your program, in order to check the contents of Statistics.txt, you must once again refresh P8 which will make the file visible in your project directory.
  22. First, using your PrintWriter object, print the following to the file:
     FILE STATISTICS 
  23. Then, print:
     Number of Lines: 
    followed by a space and the associated variable.
  24. Next, print:
     Number of Words: 
    followed by a space and the associated variable and include a new line.
  25. Lastly, close your Scanner and PrintWriter.

  26. Part Four
  27. 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.
  28. Write a for loop that iterates over all of the elements in the String array.
  29. First, assign the line at the current index to your String object.
  30. Next, write an if statement that breaks the loop if the String is null. Without this check, what exception will be generated?
  31. After the if statement, add the length of the String to your character count variable.
  32. Now, write an inner for loop to determine the type of characters in each String.
  33. 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.
  34. 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?).

  35. Part Five
  36. You are now ready to print six more lines to the output file for this project, Statistics.txt.
  37. First, using your PrintWriter object, print the following to the file:
     Number of Characters: 
    followed by your associated variable.
  38. Next print:
     Number of Uppercase: 
    followed by the appropriate variable.
  39. 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: 
  40. Make sure the last print statement includes a new line.
  41. If you have not already done so, close your Scanner and PrintWriter objects.

Output File Contents

FILE STATISTICS
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

Questions to Consider

Specifications

Grading Criteria


Submit P8.java to Checkin.


CS Banner
CS Building