I hope someone can help me with this.
I have an online Java OOP class right now, and I'm almost done with this assignment, but I've run into a problem, and the professor isn't answering my emails.
I'm trying to make a program that takes a file containing records of days and temperatures, which then eventually outputs to a separate record that has the day, highest temperature for that day, lowest temperature for that day, and average temperature for that day. We're using arrays to keep track of our data.
Most everything is working fine, but for some reason, some of the days will not get the correct lowest temperature, and will instead assign the number that the array index is initialized with at the beginning of the program.
Any help on this would be appreciated. Below is my code, as well as what the arrays look like after the program has run, and how the files look after the array has run.
Thanks in advance.
package dow.with.arrays;
import java.util.Arrays;
public class DOWWithArrays
{
public static void main(String[] args)
{ //start
InputFile inFile = new InputFile("input.txt");
OutputFile outFile = new OutputFile("output.txt");
//INITILIZATION
int day = 0;
int temp = 0;
int[] high = new int[8]; //declares an array of integers for high temps
int[] low = new int[8]; //declares an array of integers for low temps
int[] count = new int[8]; //declares an array of integers for counting days
int[] total = new int[8]; //declares an array of integers for total temp
for (day = 0; day < 8; day++) //initilization for the arrays
{
high[day] = -999;
low[day] = 999;
count[day] = 0;
total[day] = 0;
}
//tells user the DOW Temp program is starting
System.out.println("DOW Temperature Started. Please wait...");
System.out.println(Arrays.toString(high)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(low)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(count)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(total)); //GET RID OF THIS BEFORE TURN IN
while (!inFile.eof())
{ //not the end of file
day = inFile.readInt(); //read first int
temp = inFile.readInt(); //read second int
if (temp > high[day]) //assigns the current highest temperature
{ //into the correct place in the high array
high[day] = temp;
} else if (temp < low[day])//assigns the current lowest temperature
{ //into the correct place in the low array
low[day] = temp;
}
count[day]++; //counts how many temps there are in the specific day
total[day] = total[day] + temp; //calculates the total temp for each day
} //now end of file
for (day = 1; day < 8; day++)
{
outFile.writeInt(day); //write day #
outFile.writeInt(high[day]); //write high temp for that day
outFile.writeInt(low[day]); //write low temp for that day
outFile.writeInt(total[day] / count[day]); //write average temp for that day
outFile.writeEOL(); //write end of line
System.out.println(day);
} //for
System.out.println(Arrays.toString(high)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(low)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(count)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(total)); //GET RID OF THIS BEFORE TURN IN
outFile.close();
System.out.println("DOW Temperature Completed Sucessfully.");
} //stop
} //end DOW With Arrays
BEFORE:
[-999, -999, -999, -999, -999, -999, -999, -999]
[999, 999, 999, 999, 999, 999, 999, 999]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]`
AFTER:
[-999, 62, 56, 70, 61, 59, 77, 55]
[999, 55, 999, 63, 59, 999, 999, 999]
[0, 2, 2, 3, 2, 2, 1, 1]
[0, 117, 110, 200, 120, 108, 77, 55]
FILE BEFORE:
1 62
1 55
2 54
2 56
3 67
3 70
3 63
4 61
4 59
5 49
5 59
6 77
7 55
FILE AFTER:
1 62 55 58
2 56 999 55
3 70 63 66
4 61 59 60
5 59 999 54
6 77 999 77
7 55 999 55
int[] array = new int[8];0based, not1based, and a quick look at your code suggests that you may be ignoring this.