Code doesn't produce anything on screen - Doesn't give any erroreither while compiling / running.

Discussion in 'Java' started by Vasu, Oct 18, 2008.

  1. Vasu

    Vasu Guest

    Hi Friends!

    I have got the following code for converting Fahreignheit to Celsius,
    which writes the output to a file called fout and then prints the
    output from that file. It is compiled without producing any error,
    and when I try to run it do not get any error also, but it doesn't
    write anything on the screen either. When we take the output directly
    from the code (without writing it on a file and reading from there)
    works excellently, but when we take the first route the above is the
    scene. Can somebody help tell me where is the problem and what is
    solution if I want to read the results from a file. Here is the code
    I'm using :

    // Print a Fahrenheit to Celsius table

    // Write the Fahrenheit to Celsius table in a file


    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 to the FileOutputStream into a PrintStream

    PrintStream myOutput = new PrintStream(fout);

    while (fahr <= upper) { // while loop begins here
    celsius = 5 * (fahr-32) / 9;
    myOutput.println((float)fahr+" "+(float)celsius);

    fahr = fahr + step;
    } // while loop ends here

    } //try ends here

    catch(IOException e){
    System.out.println("Error: "+e);

    } // main ends here
    } //FahrToCelsius ends here

    Thanks for your help and cooperation
    Vasu, Oct 18, 2008
    1. Advertisements

  2. You could try closing the stream.
    Knute Johnson, Oct 18, 2008
    1. Advertisements

  3. Kindly disregard my previous post.

    To print to the screen, change fout to System.out in the PrintStream

    PrintStream myOutput = new PrintStream(System.out);
    Knute Johnson, Oct 18, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.