0

I have a question on how to perform a calculation on several arrays. I am trying to calculate the heat index. When I tried to calculate it, and when I ran the program, for the heat index, it just displayed 0.00000. Any help will be greatly appreciated. Below is my code:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class HeatIndex{

public static void main(String args[]) throws IOException{

    // display title
    System.out.printf("%80s %n", "Heat Index: Key West, Florida\n\n\n");

    // create months array
    String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
        "Aug", "Sep", "Oct", "Nov", "Dec"};

    // create leading blank zeros
    System.out.print("           ");

    // for each loop to display month
    for(String month : months){

        // display months
        System.out.printf("%10s", month);
    }

    // create new line
    System.out.println();

    // create line
    System.out.println("___________________________________________________"
            + "____________________________________________________________"
            + "_____________________");

    // create new line
    System.out.println();

    // create array for temp
    double[] tempA = new double[12];

    int d = 0;

    // create inFile1 variable
    Scanner inFile1 = new Scanner(new File("/Users/timothylee/KeyWestTemp.txt"));

    // while statement for inFile1
    while(inFile1.hasNextDouble()){

        // initialize array
        tempA[d++] = inFile1.nextDouble();
    }

    // display Temperature: heading
    System.out.print("Temperature:");

    // for each loop for displaying array
    for(double temperature : tempA){

        // display array
        System.out.printf("%10.1f", temperature);
    }

    // create new line
    System.out.println();

    // create inFile2 variable
    Scanner inFile2 = new Scanner(new File("/Users/timothylee/KeyWestHumid.txt"));

    // create array for humidity
    double[] humidityA = new double[12];

    int i = 0;

    // while statment for inFile2
    while(inFile2.hasNextDouble()){

        // initialize array
        humidityA[i++] = inFile2.nextDouble();
    }

    // display Humidity: heading
    System.out.print("   Humidity:");

    // for each loop for displaying array
    for(double humidity : humidityA){

        // display array
        System.out.printf("%10.1f", humidity);
    }

    // create new line
    System.out.println();

    // // perform calculation

    // create array for heat index
    double[] heatIndex = new double[12];

    int h = 0;

    // while loop for inFile1 & 2
    while(inFile1.hasNextDouble() && inFile2.hasNextDouble()){

        ////////////// initialize array
        heatIndex[h++] = -42.379 + 2.04901523 * tempA[d++] + 10.14333127 * 
                humidityA[i++] - 0.22475541 * tempA[d++] * humidityA[i++] - 
                6.83783 * Math.pow(10, -3) * Math.pow(tempA[d++], 2) - 
                5.481717 * Math.pow(10, -2) * Math.pow(humidityA[i++], 2) + 
                1.22874 * Math.pow(10, -3) * Math.pow(tempA[d++], 2) * 
                humidityA[d++] + 8.5282 * Math.pow(10, -4) * tempA[d++] * 
                Math.pow(humidityA[i++], 2) - 1.99 * Math.pow(10, -6) * 
                Math.pow(tempA[d++], 2) * Math.pow(humidityA[i++], 2);
    }

    // display Heat Index: heading
    System.out.print(" Heat Index:");

    // for each loop for displaying array
    for(double heatindex : heatIndex){

        // display array
        System.out.printf("%10.5f", heatindex);
    }

    // for each loop for calculation

}
}
2
  • 5
    I think you should go easy on using ++. Do you realize that every time you use it that variable value increases by one?
    – jny
    Nov 25, 2013 at 18:50
  • What does your input file look like? Nov 25, 2013 at 19:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.