Completed
lab is due by: tomorrow but see DELIVERABLES
below
Use the java program
Lab3.java as your base. Be sure to fill
in your name in the header.
Part 1 - Manipulating files.
1.
Into the Lab3 program,
insert the filename input.txt in the statement below:
myFileScanner = new Scanner (new File
( ) );
2.
Where will it assume the
file is?
remember that
what goes in the parentheses is a String (i.e. needs double quotes
around it)
In the same directory as Lab3.javaÏ |
3.
Now compile the program.
Did you get any compile errors?
What error did you get? Why?
Lab3.java:37: unreported exception
java.io.FileNotFoundException; must be caught or declared to be thrown |
4.
Correct the errors by adding the appropriate throw clause.
5.
Run the program. Since there is no file named input.txt, what
error message occurs?
Exception in thread
"main" java.io.FileNotFoundException: input.txt (The system cannot
find the file specified) |
6.
Insert an appropriate try catch block to catch the exception,
print an appropriate message and exit the program if the exception is thrown.
7.
Create a file on the C drive in the Temp directory named,
input.txt (it does not matter what is in the file)
8.
Alter the initialization of myFileScanner so that it contains
the absolute (complete) pathname to the file you just created. (Recall that an absolute path name begins
with the drive letter.) Write the path
name that you used in the block below.
Note that the \ is an escape character. To indicate that a \ is really a back-slash you need to use a
double \\ every time. If your filename happens to begin with a
letter that has meaning following a \ (such as \n which means newline) you won’t get an error message.
“C:\\Temp\\input.txt” |
9.
Hard-coding a filename into a program is evil. Alter your
program so that it gets the correct filename from the user, using the keyboard
scanner already in the Lab3 program.
10.
Check that your program works when you give it the correct
filename
11.
Now type in an incorrect filename. What error message is output
by your try/catch block?
the file whose name you entered could not be found – which is the
error message I expected |
12.
Alter your try catch block so that instead of quitting, your
program tries again until the desired filename is entered.
note that issues here are
i.
whether to
embed try/catch in loop or loop in try/catch
ii.
Which kind of loop to use and what difference it
makes
This part of the lab asks you to actually
process the data in the following way.
Note that may only read the file data once.
13.
You are to:
a. Find and display
(with appropriate label), the smallest int in the file.
b. Determine whether
the value 13 is in the file and print a message telling how many times it
occurs. Java code
can be downloaded here.
14.
Create your own test file consisting of a series of ints and
test your program. Your file should be
named <yourfirstname>.txt
15.
Now copy the file numbers.txt to your working directory.
16.
The file was supposed to contain only ints but professors Harris
and Adams were tired when they created it so it has some junk in it. You must deal with the file as it
exists. Any bad data should be
ignored. Alter your program to
accommodate the bad data.
17.
Now run another test with the file numbers2.txt and insure that
it works properly.
DELIVERABLES