Program 20.1: Write the Fahrenheit to Celsius table in a file

Program 20.1 demonstrates this via a modified Fahrenheit to Celsius conversion program from Chapter 4.

import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;

class FahrToCelsius  {

  public static void main (String args[]) {

    double fahr, celsius;
    double lower, upper, step;

    lower = 0.0;    // lower limit of temperature table
    upper = 300.0;  // upper limit of temperature table
    step  = 20.0;   // step size

    fahr = lower;
  
    try {

      FileOutputStream fout =  new 
        FileOutputStream("test.out");

      // now chain the FileOutputStream to a PrintStream
      PrintStream myOutput = new PrintStream(fout);
  
      while (fahr <= upper) {  // while loop begins here
        celsius = 5.0 * (fahr-32.0) / 9.0;
        myOutput.println(fahr + " " + celsius);
        fahr = fahr + step;
      } // while loop ends here
  
    }  // try ends here
    catch (IOException e) {
      System.err.println("Error: " + e);
      System.exit(1);
    }
  
  } // main ends here

}

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home