CSU Banner

CS 150, Fall 2017

Lab 11 - Introduction to File Input/Output and the Start of P9

Monday - November 6th, 2017

Objectives of this Lab

  1. Begin the code necessary for P9,
  2. which includes file input using a Scanner, and
  3. file output using a PrintWriter with a try-catch block, as well as
  4. more practice with while loops, and
  5. complete the first two requirements of P9.

Getting Started

Create a new Java Project named P9, and make a class named P9. Once complete, P9 will calculate file statistics of the Gettysburg address, such as the number of lines and words in the text. Today's lab asks you to write code to determine two of the eight categories of statistics your program will eventually calculate. Additionally, if you do not already know how to do so, please listen closely when your TA demonstrates how to add a text file to a Java project directory.

Today's Assignment

    Part One
  1. Create a new Java Project in Eclipse called P9. Make a class also called P9 and check the box to have Eclipse add the main method for you.
  2. It is recommended you read through the rest of the instructions first before beginning to write any code. Once you understand what you are expected to do in this assignment, you can better organize your thoughts and make a more efficient plan of action. Additionally, there is not a description accompanying each of the variables below; therefore, reading the instructions first will also help you name your variables in such a way so you can easily understand what each one is for as you program. You may also need to read the specifications for P9 as well, in order to do this.
  3. First, save the Gettysburg.txt file to your P9 project main directory from here. To do this, right click on the link and click either 'Save link as' or 'Download as'. Then save the file to your P9 project directory. DO NOT save the text file in your source folder.
  4. Second, 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) {
      } // 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.
  5. Declare and initialize two File objects, one with the source as args[0], and the other as args[1].
  6. Declare and initialize a Scanner object to read from the file object related to args[0].
  7. Declare and initialize a PrintWriter object to write to the file object pertaining to args[1].
  8. Declare and initialize a String array to a size of 25 elements.
  9. Declare and initialize two integer variables to 0.
  10. Set up the program arguments for P9 to read Gettysburg.txt Statistics.txt - make sure there is a space in between file names and that the names are on the same line.

  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. Do not close this Scanner.
  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 a 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.
  22. First, using your PrintWriter object, print the following to the file:
  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.

Questions to Consider

Output File Contents

Number of Lines: 21
Number of Words: 274

CS Banner
CS Building