/*
         SI411 Operating Systems Fall AY07
               Java Programming I/O Demo
             Due: This is for demo purposes

Overview: This lab shows how to read text files 
line by line, write to output files, manipulate strings, the use
StringTokenizer for parsing a line of text, and
preparing integers for IO by converting integers to
strings and back again.

*/

import javax.swing.JOptionPane;  // needed for GUI I/O
import java.io.*; // needed for file I/O
import java.util.*; // StringTokenizer

public class IODemo {
  // instance variables go here and have "class scope"
  // File I/O instance variables

  private BufferedReader inFile;
  private BufferedWriter outFile;

public static void main( String args[]){

 IODemo theProgram = new IODemo();  // create class instance
 theProgram.runProgram(); // "launch" the program
}

public void runProgram(){

  int number1, number2, sum;
  String firstNumber = new String();
  String secondNumber = new String(); // strings entered by user

  // read in two numbers from user as strings using GUI I/O
  firstNumber  = JOptionPane.showInputDialog("Enter 1st int");
  secondNumber = JOptionPane.showInputDialog("Enter 2nd int");

  number1 = Integer.parseInt(firstNumber); //convert str to int
  number2 = Integer.parseInt(secondNumber);

  sum = number1 + number2; // find sum of values entered

  // display the results using GUI I/O
  JOptionPane.showMessageDialog(null, "The sum is " + sum,
          "Results", JOptionPane.PLAIN_MESSAGE);

  // demo file I/O, note that inFile and outFile are not
  // passed as arguments since these are instance variables
  openFiles();
  workWithFiles();
  closeFiles();
 } // runProgram

public void openFiles(){
  String inputFileName = new String("input.txt");
  String outputFileName = new String("output.txt");

   try {
      inFile = new BufferedReader(new FileReader(inputFileName));
      outFile = new BufferedWriter(new FileWriter(outputFileName));
    } // end try

    catch (FileNotFoundException e) {
       System.out.println("File Not Found!");
       e.printStackTrace();
    }  // end catch

    catch (IOException e) {
       System.out.println("IO Problem!");
       e.printStackTrace();
    }  // end catch
  } // end openFiles

public void closeFiles(){
 try{
   inFile.close();
   outFile.close();
 }
 catch(IOException e){
    System.out.println("Problem closing files!");
    e.printStackTrace();
 }
}

public void workWithFiles(){
 // this method reads inFile line by line
 // and writes each line to an output file with any
 // occurance of 1981 replaced with 1983.  Demos
 // file I/O, use of StringTokenizer for chopping
 // up a line of text, and converting strings to
 // integers and back again.

  String line =  "";
  try {
   for (;;) { // read from file until null returned
     line = inFile.readLine();
     if (line == null) break;
     System.out.println(line);   // echo line to screen

     // use StringTokenizer to break up line
     StringTokenizer tokens = new StringTokenizer (line);
     while (tokens.hasMoreTokens()){
        String oneToken = new String(tokens.nextToken());
        // demo conversion of String to int, and back
        if (oneToken.equals("1981")){
            int i = Integer.parseInt(oneToken);
            i += 2;  // turns 1981 to 1983
            oneToken = String.valueOf(i);
        }
        outFile.write(oneToken + " ");

     } // end while
     outFile.newLine();
    } // end for

    } // end try
    catch (IOException e){
       System.out.println(e.toString());
    } // end catch
  } // end workWithFiles
 }// end IODemo





