Untitled Document
Here are a few practice problems on Exceptions and Files
1. Write a program that has a method that prints the first 6 entries of an
array. Have the main method call this with an array
that has only 5 elements. Use a try-catch statement
to catch the resulting exception.
2. Write a program that reads from the keyboard and prints lines of text. In
between the reading of a line and
the printing of the line send this line to a method
String
filter(String line)
If the argument line contains the substring "bob"
(just look for the index of "bob" in line), have the filter method
throw
a Runtime exception -- we don't want any bobs. If line
does not contain "bob", just have the filter method return the line.
Finally, your call to filter should catch any Runtime
exception that is thrown by this method.
3. Write a program that reads a list of integers from the keyboard. As each
number is read it should be stored in a file
called "data.txt". Use the FileWriter
class to do this. Write another program that reads integer data out of file
data.txt and prints the average value of this
data.