created: 3/21/2006; revised: 08/20/2014, 06/18/2015, 08/22/2016, 02/16/25
Instructions: Each question has something left out. A blank represents the missing word(s). Think of the word or phrase that completes the sentence, then click on the buttons to see if you are correct.
1.
When a Scanner
is connected to a disk file the
characters from the file form an
stream.
2.
The following connects a Scanner
to the file myStuff.txt
.
File file = new File ( ); Scanner scan = new Scanner ( );
3.
When nextInt()
scans text integers, integers should be
delimited (separated) with
or line separator characters.
4.
When using the nextInt()
method of Scanner
,
may input integers have a leading +
?
5.
When using the nextInt()
method of Scanner
,
may input numbers have a leading -
?
6.
If something goes wrong when Scanner
tries to open a file for reading, an
is thrown.
7.
If nextInt()
encounters characters that cannot be converted into
an int
an
is thrown.
8.
What value does hasNextInt()
return if a Scanner
has reached the end of a file?
9. What is the name for a group of characters delimited by blanks or end of line characters?
What "next" method is used to determine if there is another such group in the input stream?
What method is used to read such a group from input stream?
10. Complete the following program so that it asks the user for a file name, then opens that file as an input stream.
import java.util.Scanner; class OpenInput { public static void main ( String[] args ) { Scanner user = new Scanner( ); System.out.print("Enter a filename: "); String fileName = user..trim() ; File file = new File( ) ; Scanner disk = new Scanner( ); . . . . . } }
11.
The following connects a PrintWriter
to the file myOutput.txt
.
PrintWriter print = new PrintWriter ("");
12.
If you use PrintWriter
to send output to a file that already exists
what happens to the characters that are already in the file?
They are
with the new characters.
13.
If a PrintWriter
attempts to open a file for which you do
not have writing permission, an is thrown.
14.
If a main()
method uses PrintWriter
it should start out
public static void main ( String[] args )
15.
At the end of writing a file with PrintWriter
the file should be .
End of the Exercise. If you want to do it again, click on "Refresh" in your browser window.