COSC 236

Lab 9

The goal of this lab is to get used with basic Java notions introduced so far and  file processing (discussed in Lecture #8). Take a look at the examples and the sample programs in the lecture notes and try to apply the same concepts and style when writing your own programs. For this lab you will write 2 programs, given below. For all programs you have to write 2 versions: one that throws the FileNotFoundException exception and one that handles the exception (try/catch). Also, make sure that your programs handle (consume) unwanted input. Test your programs in a similar fashion to the output samples listed below.



First, create a file called "
input.txt" that contains a sequence of integer values separated by one or more spaces or returns. You can use the Notepad to create the file. When you save the program choose the "text file" option and name your file "input" because Notepad will automatically append the extension ".txt".  Put this file in the folder you are going to use for the following Java programs (anyway, know where your text file is located, so that you can give a full path in case it isn't in the same folder as your Java program). You will use this file in the programs below.


1. Write a Java program that reads the file "input.txt" and calculates and prints the average of all integer numbers in this file.

SAMPLE OUTPUT:

throws  FileNotFoundException

try/catch

OUTPUT SAMPLE #1 for input.txt: 1 2 3 4 5 6 x x x 8 12 x x 34 x x 10 20
Number # 1 is: 1
Number # 2 is: 2
Number # 3 is: 3
Number # 4 is: 4
Number # 5 is: 5
Number # 6 is: 6
Number # 7 is: 8
Number # 8 is: 12
Number # 9 is: 34
Number # 10 is: 10
Number # 11 is: 20
The average of 11 numbers/file = 9.55

OUTPUT SAMPLE #2 for input.txt: xxxxx x x x x xx x x x x x
The file doesn't contain any integers. Exit program!

OUTPUT SAMPLE #1 for input.txt: 1 2 3 4 5 6 x x x 8 12 x x 34 x x 10 20
Please input the name of the file to be opened: input.tx
--- File Not Found! ---

OUTPUT SAMPLE #2 for input.txt: 1 2 3 4 5 6 x x x 8 12 x x 34 x x 10 20
Please input the name of the file to be opened: input.txt
Number # 1 is: 1
Number # 2 is: 2
Number # 3 is: 3
Number # 4 is: 4
Number # 5 is: 5
Number # 6 is: 6
Number # 7 is: 8
Number # 8 is: 12
Number # 9 is: 34
Number # 10 is: 10
Number # 11 is: 20
The average of 11 numbers/file = 9.55

OUTPUT SAMPLE #3 for input.txt: xxxxx x x x x xx x x x x x
Please input the name of the file to be opened: input.txt
The file doesn't contain any integers. Exit program!


2. Write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt."

SAMPLE OUTPUT: 

throws  FileNotFoundException

try/catch

OUTPUT SAMPLE #1 for input.txt: xx x xx 10 12 1 3 5 34 x 2 5 7 9 x x 44
Found 11 numbers/file.
Open the file output.txt to get the output.
output.txt: 10 12 34 2 44 

OUTPUT SAMPLE #2 for input.txt: xx xx xx  x  x x xxx xxx
The file doesn't contain any integers. Exit program!

OUTPUT SAMPLE #1: 
Please input the name of the file to be opened: input.tx
--- File Not Found! ---

OUTPUT SAMPLE #2 for input.txt: xx x xx 10 12 1 3 5 34 x 2 5 7 9 x x 44
Please input the name of the file to be opened: input.txt
Found 11 numbers/file.
Open the file output.txt to get the output.
output.txt: 10 12 34 2 44

OUTPUT SAMPLE #3 for input.txt: xx xx xx  x x xxx x xx x x x
Please input the name of the file to be opened: input.txt
The file doesn't contain any integers. Exit program!


Notes:
A. The lab will NOT be graded, do NOT hand anything in to the instructor.
B. The lab should be completed by the start of the next scheduled lab class.
Save the .java files on your disk and e-mail them (attachments) to Vishal Patel at vpatel12@students.towson.edu

Very important: Make sure that you have COSC 236.section, your name, and Lab#9 in the Subject box of your e-mail.
C. In case you have any problems, contact the TA or the instructor for assistance.