im trying to get the same heat index that prints int the program to write to a file. i have copied the procedure from another program that worked correctly. any help is appreciated.

import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class HeatIndex
{
   public static void main(String [] args) throws IOException
   {

       Scanner inFile = new Scanner(new File("BIRMINGHAM AP,AL temp record test.txt"));

       double sumTemp = 0;
       double averageTemp = 0;
       double sumHumidity = 0;
       double averageHumidity = 0;
       double sumHeatIndex = 0;
       double averageHeatIndex = 0;
       int counter = 0;

       PrintWriter outFile = new PrintWriter (new File("writeFile.txt"));

       double bhamTemp[] = new double[12];
       for (int bhamTempIndex = 0; bhamTempIndex <= 11;)
        {

            bhamTemp[bhamTempIndex] = inFile.nextDouble();
            sumTemp += bhamTemp[bhamTempIndex];
            averageTemp = sumTemp/12;
            bhamTempIndex++;
        }    

        Scanner  inFile2 = new Scanner(new File("13876BIRMINGHAM AP,AL Humidity Record Test.txt"));
        double bhamHumidity[] = new double [12];
        for (int bhamHumidityIndex = 0; bhamHumidityIndex <= 11;)
        {
            bhamHumidity[bhamHumidityIndex] = inFile2.nextDouble();
            sumHumidity += bhamHumidity[bhamHumidityIndex];
            averageHumidity = sumHumidity/ 12;
            bhamHumidityIndex++;
        }

         double heatIndex[] = new double [12];
         for (int indexCounter = 0; indexCounter <= 11;)
         {

             heatIndex[indexCounter] = heatIndex[indexCounter] = -42.379 + (2.04901523 * bhamTemp[indexCounter]) + (10.14333127 * bhamHumidity[indexCounter]) - (.22475541 * bhamTemp[indexCounter] * bhamHumidity[indexCounter]) - (6.83783 * .001 * bhamTemp[indexCounter] * bhamTemp[indexCounter]) - (5.481717 * .01 * bhamHumidity[indexCounter] * bhamHumidity[indexCounter]) + (1.22874 * .001 * bhamTemp[indexCounter] * bhamTemp[indexCounter] * bhamHumidity[indexCounter]) + (8.5282 *  .0001 * bhamTemp[indexCounter] * bhamHumidity[indexCounter] * bhamHumidity[indexCounter]) - (1.99 *  .000001 * bhamTemp[indexCounter] * bhamTemp[indexCounter] * bhamHumidity[indexCounter] * bhamHumidity[indexCounter]);
             sumHeatIndex += heatIndex[indexCounter];
             averageHeatIndex = sumHeatIndex/12;
             outFile.println(heatIndex[indexCounter]);
            indexCounter++;

          }




        System.out.println("                                                           Heat Index: Birmingham, AL");
        System.out.println();
        System.out.println("                                                                      Months");
        System.out.println("\n");
        System.out.println("            Jan       Feb       Mar       Apr       May       Jun       Jul       Aug       Sep       Oct       Nov       Dec       Avg");
        System.out.println("*******************************************************************************************************************************************");
        System.out.printf("Temp F      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f\n", bhamTemp[0],bhamTemp[1],bhamTemp[2],bhamTemp[3],bhamTemp[4],bhamTemp[5],bhamTemp[6],bhamTemp[7],bhamTemp[8],bhamTemp[9],bhamTemp[10],bhamTemp[11], averageTemp);

        System.out.printf("Humidity    %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f\n", bhamHumidity[0],bhamHumidity[1],bhamHumidity[2],bhamHumidity[3],bhamHumidity[4],bhamHumidity[5],bhamHumidity[6],bhamHumidity[7],bhamHumidity[8],bhamHumidity[9],bhamHumidity[10],bhamHumidity[11],averageHumidity);

        System.out.printf("HI         %4.1f     %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f      %4.1f     %4.1f\n", heatIndex[0],heatIndex[1],heatIndex[2],heatIndex[3],heatIndex[4],heatIndex[5],heatIndex[6],heatIndex[7],heatIndex[8],heatIndex[9],heatIndex[10],heatIndex[11], averageHeatIndex);
        System.out.println("*******************************************************************************************************************************************");




    }
}

Dani AI

Generated

The symptom described (console shows the heat-index table but the output file is empty or incomplete) is almost always a file-flushing/closing or path issue. was right to suggest flushing, but flushing alone is brittle. The reliable fix is to ensure the writer is closed (so buffers are flushed) even if an exception happens, and to verify where the JVM actually created the file.

A concise checklist to fix and harden the code:

  • Use try-with-resources so the writer is always closed automatically.
  • Prefer a writer that lets you set the charset explicitly (avoid platform-default encoding surprises).
  • Verify the working directory (System.getProperty("user.dir")) or use an absolute path so the file appears where you expect.
  • If you use PrintWriter, check its error state with checkError() because PrintWriter does not throw IOExceptions on write failures (see the PrintWriter javadoc).

Small example pattern (adapt to your formatting code):

try (java.io.PrintWriter pw = new java.io.PrintWriter(
        new java.io.OutputStreamWriter(
            new java.io.FileOutputStream("writeFile.txt"), java.nio.charset.StandardCharsets.UTF_8))) {
    // format and write lines, e.g. pw.printf(...);
}

If the file is still empty:

  • Confirm the write loop actually runs (put a quick console log or breakpoint).
  • Check file permissions and antivirus/locker software.
  • Call pw.checkError() after writing to detect silently swallowed errors.

References: PrintWriter behavior and checkError are documented in the Java API (PrintWriter (Java SE 8)). For ensuring proper close semantics, see the try-with-resources tutorial (Try-with-resources).

This addresses the immediate I/O issues while keeping the formatted table approach you use for console output.

Recommended Answers

All 3 Replies

Firstly, the start of the CODE block in posting your code here should be without '/' to display the formatted code correctly.

The way you set-up your Printwriter is without automatic flushing. You need to use flush() method after you write something into the file. Have a look on the documentation on how to set up automatic flushing.

are you just writing to the file once?

Firstly, the start of the CODE block in posting your code here should be without '/' to display the formatted code correctly.

The way you set-up your Printwriter is without automatic flushing. You need to use flush() method after you write something into the file. Have a look on the documentation on how to set up automatic flushing.

are you just writing to the file once?

outFile.println(heatIndex[indexCounter]);
outFile.flush();
outFile.println(heatIndex[indexCounter]);
outFile.flush();

thank you for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.