created: 3/21/2006


Fill in the Blanks

Instructions:   Each question has something left out. A button 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, 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 charaters? What "next" method is used to determine if there is another one in the input stream? What method is used to read the one in 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(.trim() ;
    Scanner disk = new Scanner(  );

    . . . . .

  }
}


11. The following connects a PrintStream to the file "myOutput.txt" .

File file = new File ("");
PrintStream print = new PrintStream ( );


12.   If you use PrintStream 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 PrintStream attempts to open a file for which you do not have writing permission, what happens?


14.   If a main() method uses PrintStream it should start out

public static void main ( String[] args ) 

15.   At the end of writing a file with PrintStream the file should be .



End of the Exercise. If you want to do it again, click on "Refresh" in your browser window. Click here to go back to the main menu.