Question

I'm working on a program to calculate heat index, and am supposed to make heavy use of foreach loops. However, when I print to the terminal, it doesn't come out right. I've already spent two days on this, but I still cannot find out why it's still doing this. Thanks for any help/advice!

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

public class HeatIndex {

  public static void main(String[] args) throws IOException {
    Scanner keyWestHumidScan = new Scanner(new File("KeyWestHumid.txt"));
    Scanner keyWestTempScan = new Scanner(new File("KeyWestTemp.txt"));

    int counter1 = 0;
    int counter2 = 0;
    double[] keyWestHumid = new double[12];
    double[] keyWestTemp = new double[12];
    String header1 = "                                     Heat index: Key West, Florida                              ";
    String header2 = "\n                                                Months                                        \n          ";
    String[] months = {"    Jan   ", "Feb   ", "Mar   ", "Apr   ", "May   ", "Jun   ", "Jul   ", "Aug   ", "Sep   ", "Oct   ", "Nov   ", "Dec   ", "Avg   \n"};
    String header3 = "*****************************************************************************************";
    String temp = "Temp (F)      ";
    String humid = "Hudimitiy (%)    ";
    String heatIndexHeader = "HI (F)      ";


    //read keyWestHumid into array
    while (keyWestHumidScan.hasNext()) {

      String data1_parse = keyWestHumidScan.next();
      double data1 = Double.parseDouble(data1_parse);
      keyWestHumid[counter1] = data1;
      counter1++;


    }
    //read keyWestTemp into array
    while (keyWestTempScan.hasNext()) {
      String data2_parse = keyWestTempScan.next();
      double data2 = Double.parseDouble(data2_parse);
      keyWestTemp[counter2] = data2;
      counter2++;

    }
    System.out.println(header1);
    System.out.print(header2);
    for (String headData : months) {
      System.out.print(headData);
    }
    System.out.println(header3);
    System.out.print(temp);
    for (double data : keyWestTemp) {
      System.out.print(keyWestTemp + "     ");
    }
    System.out.println();
    System.out.print(humid);
    for (double data : keyWestHumid) {
      System.out.print(keyWestHumid + "     ");
    }
    System.out.println();
    System.out.print(heatIndexHeader);
    counter1 = 0;
    counter2 = 0;
    for (int counter3 = 0; counter3 <= 12; counter3++) {
      double heatIndex = (-42.379 + (2.04901523 * keyWestTemp[counter1]) + (10.14333127 * keyWestHumid[counter2]) - (0.22475541 * keyWestTemp[counter1] * keyWestHumid[counter2]) - (0.00683783 * (keyWestTemp[counter1] * keyWestTemp[counter1])));
      heatIndex = heatIndex + (-0.05481717 * (keyWestHumid[counter2] * keyWestHumid[counter2]) + (0.00122874 * (keyWestTemp[counter1] * keyWestTemp[counter1] * keyWestHumid[counter2])) + 0.00085282 * keyWestTemp[counter1] * (keyWestHumid[counter2] * keyWestHumid[counter2]) - (0.00000199 * (keyWestTemp[counter1] * keyWestTemp[counter1]) * (keyWestHumid[counter2] * keyWestHumid[counter2])));
      counter1++;
      counter2++;
      counter3++;
      System.out.print(heatIndex + "      ");
    }

  }
}
Was it helpful?

Solution 2

To start, your code is pretty hard to read. Make sure you are following good code style (indent 4 spaces inside a for loop block, for example). Read through this: http://www.oracle.com/technetwork/java/codeconv-138413.html

From glancing through your code, could the porblem be here:

for(double data:keyWestTemp) {
System.out.print(keyWestTemp + "     ");
}

and here:

for(double data:keyWestHumid) {
System.out.print(keyWestHumid + "     ");
}

I think you want to be using data in the print line, instead of keyWestHumid/Temp. data is the object, keyWestHumid/Temp is your whole array.

You should really be using an IDE such as Eclipse or Netbeans, it will make your life much easier. Right away Eclipse tells you that data isn't used in these loops, which is a problem.

OTHER TIPS

You are printing out the collection of objects, and not the individual objects themselves. For instance: for (double data : keyWestTemp) { System.out.print(keyWestTemp + " "); }

Should actually be: for (double data : keyWestTemp) { System.out.print(data + " "); }

Use System.out.printf to format your text to be displayed on the console.

System.out.printf("%10s%10s", "Number", "String");
System.out.println("");
System.out.printf("%10d", 10);
System.out.printf("%10s", "Hi there");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top