created: 3/21/2006; revised: 08/20/2014, 06/18/2015, 08/22/2016

Chapter 23 Fill in the Blanks

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.

go to home page