go to previous page   go to home page   go to next page

Answer:

Yes.

The newline character is skipped over by Scanner.nextInt().


Review: PrintWriter

import java.io.*;

public class WriteTextFile 
{

  public static void main ( String[] args ) throws IOException
  {
    PrintWriter output = new PrintWriter( "myOutput.txt" );

    output.println( "The world is so full"  );  
    output.println( "Of a number of things,"  );  
    output.println( "I'm sure we should all" );  
    output.println( "Be as happy as kings."  );  

    output.close();
  }
}

Here is a program you have seen before.

new PrintWriter( "myOutput.txt" ); creates the file myOutput.txt and creates a PrintWriter object. Then the program writes several lines of characters to that file using println().

PrintWriter output = new PrintWriter("myOutput.txt");

An IOException is thrown if there is a problem creating the file.

If the file already exists its contents will be destroyed. This does not throw an IOException. If the user does not have permission to alter the file, an IOException is thrown and the file is not altered. Operating systems implement file permissions where files can be made read only.


QUESTION 9:

Could a try/catch stucture be used in this program?


go to previous page   go to home page   go to next page