//  File:  ConsoleIO\ConsoleIO.java

import java.io.*;
import java.util.*;

/**
 * This short example shows how to do two things: <br>
 * 1. Read a sequence of data items from the keyboard (console) <br>
 * 2. Read a sequence of data items from a file <br>
 * 
 * NOTE:  Java 1.5's Scanner class has made this example obsolescent
 * 
 * @author Bary W Pollack
 * @version Sept 30, 1999
 * @see main
 * @see sInfFile
 */

public class ConsoleIO {

    /**
     * Name of the input file to be read in the second portion of the demo
     * 
     * @see main
     */
    
    private static final String sInFile = "input.txt";

    /**
     * The main method -- demonstrate the following: <br>
     * 1. Show how to read a sequence of data items from the keyboard <br>
     * 2. Show how to read a sequence of data items from a file
     * 
     * @param argv unused
     * @see sInFile
     */
    
    public static void main (String [] argv) {
        System.out.println("\nDemonstrate reading from the keyboard...\n"
                         + "input a sequence of integers separated by whitespace\n"
                         + "terminate the input by inputing a value < 0.\n");
        
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
            StringTokenizer stok;
            
            String str = br.readLine();
            stok = new StringTokenizer(str);
            
            int n = 0;
            
            do {
                try {
                    n = Integer.parseInt(stok.nextToken());
                    System.out.println("  n " + n);
                    
                } catch (NoSuchElementException e) {
                    str = br.readLine();
                    stok = new StringTokenizer(str);
                }
            } while (n >= 0);
            
            System.out.println("\n** End of Keyboard Input **\n\n"
                             + "Now read doubles from a file until EOF...\n");
            
            FileReader reader = new FileReader(sInFile);
            StreamTokenizer tokens = new StreamTokenizer(
                                         new BufferedReader(reader));
                                         
            int next;
            while ((next = tokens.nextToken()) != tokens.TT_EOF) {
                double d = tokens.nval;
                System.out.println("  d = " + d);
            }
            
            reader.close();
            
        } catch (IOException e) {
            System.out.println("\nGot an IOException: " + e);
        }
        
        System.out.println("\n** Fini **");
    }
    
}

/* Execution output...

NOTE:  I typed:    1 2 3 4
                     5 6 7
                      8   9
                   -10
                   
at the keyboard...


Demonstrate reading from the keyboard...
input a sequence of integers separated by whitespace
terminate the input by inputing a value < 0.

  n 1
  n 2
  n 3
  n 4
  n 5
  n 6
  n 7
  n 8
  n 9
  n -10

** End of Keyboard Input **

Now read doubles from a file until EOF...

  d = 11.0
  d = 22.0
  d = 33.0
  d = 44.0
  d = 55.0

** Fini **

..... */
