ICS 141 Programming Fundamentals

Lab 10 – 15 points

Due Tuesday, April 27 (accepted through May 4)

 

 

You must attempt to complete the lab tonight.  If you cannot, you may work on it outside and submit it by the beginning of the next class.  But I will allow that only if you work on the problem in the assigned lab time.

 

Goals: To learn exceptions, and files.

 

Part I: Understanding the Problem:

 

In this lab, you are going to write an application that uses files as input and output.  The program reads input data from one file and does some processing (adds the numbers read in), then spills the result to another file.

 

The code segments given to you have no error checking.  One of your tasks is to supply the error checking by using try-catch structure.  By doing this, you are making the program more robust so that it will not terminate in the middle of the run. 

 

The other task in this lab is to supply the missing code. 

 

You are given a good part of the code of a non-GUI program that allows the user to enter the names of two files as command line arguments. The first of these files is read by the program and the second file is for output.

 

The program uses the StringTokenizer class to break a sentence into tokens.  A token is a string of characters separated by delimiter characters (or simply delimiters).  The StringTokenizer is supplied by the Java Class Libraries.  An instance of a StringTokenizer is created with a string.  The default delimiters are the whitespace characters (space, tab, newline, and return).

 

To run this program, you will use DOS command line.  You will use TextPad to compile the program as previous labs.

 

Here are the steps that you should follow:

  1. Using TextPad, create an input file named “inData1.txt” and save it in the directory of Lab10.  The content of the file will be:

This is the input data from file inData1.txt

3 5 7 10

9 13 44 33

  1. Create an input file named “inData2.txt” by using notepad and save it in the directory of Lab10.  The content of the file will be:

This is the input data from file inData2.txt

3 5 notNumber 10

9 13 44 33

  1. Fix the syntax errors and finish the following program according to the requirements.

 


import java.io.*;

import java.util.*;

 

public class FileOperations  {

 

       private BufferedReader reader;

       private PrintWriter writer;

 

       private void processData(String inputFile, String outputFile) {

 

              String line;

              String token;

 

              int total = 0;

 

              reader = new BufferedReader(new FileReader(inputFile));

              // open writer. 

 

// Also, put previous two lines

              // into a try-catch block to check for an IOException error.

              // In the catch block, you should output the type of exception

// using System.out.println and then return. 

 

              line = reader.readLine()

              System.out.println(line);

             

while ((line = reader.readLine()) != null) {

 

                  StringTokenizer tokenizer = new StringTokenizer(line);

                  System.out.println(line);

 

                  while (tokenizer.hasMoreTokens()) {

                       token = tokenizer.nextToken();

                       int number = Integer.parseInt(token);

// put the line above into a try-catch block to check for

// NumberFormatException error.

              // In the catch block, you should output the type of

// exception using System.out.println and

              // then return. 

                    

                       total += number;

    }

              }

 

              System.out.println();

              System.out.println(“The output to file: “ + outputFile);

              System.out.println(“The total is: “ + total);

              Write.println(“The total is: “ + total);

 

              writer.close();

       }

 

       public static void main(String[] s) {

 

              if (//the parameter array does not have  

    //two parameters.  Hint: use s.length) {

                     System.out.println("Need file names");

                     System.exit(0);

              }

 

              new FileOperations().processData(s[0], s[1]);

       }

}

 

Part II: Running the program

 

After you get the program to compile, bring up the DOS command window by doing the following:

 

Click Start menu button

Choose Run

Type cmd

 

Change directories using the cd command until your default directory is the one in which your Lab 10 program resides.

 

Run the program three times, using these commands.  Observe what happens when you run the program.   Explain what is displayed on the screen and what is stored in the output files. 

 

1.  java FileOperations inData1.txt outData1.txt

 

On screen:

 

 

 

 

 

 

 

In file:

 

 

 

 

 

 

 

2.  java FileOperations inData2.txt outData2.txt

 

On screen:

 

 

 

 

 

 

 

In file:

 

 

 

 

 

 

 

3.  java FileOperations inData1.txt

 

On screen:

 

 

 

 

 

 

 

In file:

 

 

 

 

 

 

 

Part III.  Hand in

 

1.       A diskette with your working program

2.       A written explanation of what happens when you run the program.