/**
* This program
has no try/catch block and requires
* the use of "throw" to
compile.
*
* THINGS TO TRY
* Compile and run this file and see what
happens when
* you enter the name of a file that has
5 integers in it.
* Then, run it
and enter the name of a non-existent file.
* Then delete
the "throws" and recompile it and see
* what happens.
*
* @author ????
- modified by Elizabeth Adams
* @version ???
- September 5, 2008
*/
import java.util.Scanner;
import java.io.*;
public class ReadFromFile2
{
public static void main (String []
args) throws IOException
{
String fileName; // will hold the name of the file to
be read from
Scanner fileScanner;
Scanner keyboard;
int value;
// needed to get filename from
user sitting at keyboard
keyboard = new Scanner (System.in);
System.out.println
//
prompt to user at keyboard
(" Please enter name of file
holding integers and hit return ");
fileName =
keyboard.nextLine();
System.out.println // echo of user's input
(" The name of the file you want
to open is " + fileName);
fileScanner
= new Scanner (new File (fileName));
// if file is found, read numbers from file as long as there are
// numbers in
the file
while (fileScanner.hasNextInt())
{
value = fileScanner.nextInt();
System.out.println // echo the values picked up
(" The value you got from the file
is " + value);
} // end while
} // end main
} // end class