0

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
5
  • 2
    That's a lot of code and input/output; not a lot of explanation of why it is wrong. You would be best off using a debugger to step through the code. Commented Aug 4, 2018 at 19:15
  • I second what @AndyTurner states above. Also please have a look at this great reference: How to debug small programs Commented Aug 4, 2018 at 19:16
  • Thank you for that. I'll take a look at it. Sorry for the super long post! I'll try better in the future. Commented Aug 4, 2018 at 19:16
  • I'd like to add something. @Josh try to write elegant code. If you can simplify something, then do it. There is no reason to create the arrays the way that you do and it looks ugly. Try doing it like this instead: int[] array = new int[8]; Commented Aug 4, 2018 at 19:19
  • Also arrays are 0 based, not 1 based, and a quick look at your code suggests that you may be ignoring this. Commented Aug 4, 2018 at 19:21

4 Answers 4

3

If - Else if...if the if expression is true, the else if is ignored. I think the mistake is here:

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;
}

try to remove else:

if (temp > high[day]) //assigns the current highest temperature
{                     //into the correct place in the high array
    high[day] = temp;
}

if (temp < low[day])//assigns the current lowest temperature
{                        //into the correct place in the low array
            low[day] = temp;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for helping, although I will in future try to debug my program, as I was not aware that was even a thing that was possible. My professor has NOT taught me I could do that.
You might also want to point out that you can use Integer.MAX_VALUE and MIN_VALUE instead of arbitrary values like 999 and -999. And you can use Arrays.fill to put the same value into every element of an array instead of looping explicitly.
2

You already have an answer from Frighi; I just wanted to point out some alternatives.

The problem with the current code comes when you first encounter a day, since the code will not update both low and high.

You can detect this explicitly by looking at the count for a day: if it is zero, update both:

if (count[day] == 0) {
  low[day] = high[day] = temp;
} else if (temp > high[day]) {
  high[day] = temp;
} else if (temp < low[day]) {
  low[day] = temp;
}

Alternatively, you can simply use min and max, and no (explicit) conditions:

high[day] = Math.max(high[day], temp);
low[day] = Math.min(low[day], temp);

Comments

0

The problem is the else; remove it:

if (temp > high[day]) {
    high[day] = temp;
}

if (temp < low[day]) {
    low[day] = temp;
}

Consider if there’s only 1 temperature for the day, it will be both the highest and the lowest.

Comments

0

The problem you have is with the algorithm for determining the lowest and highest temperature for a day. Specifically this part:

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;
}

Should be:

if (temp > high[day]) //assigns the current highest temperature
{                     //into the correct place in the high array
    high[day] = temp;
}

if (temp < low[day])//assigns the current lowest temperature
{                        //into the correct place in the low array
    low[day] = temp;
}

You could have figured that out by yourself if you just followed the flow of the code in your head or better, with a debugger, for the first case where the behavior was wrong, day number 2. Or the simpler one, day number 7.

Also, for this part of the code specifically, look into Math.min/Math.max functions and maybe Integer.MIN_VALUE/Integer.MAX_VALUE and +=.

Also, you should try to find a way not to hard-code the number of days in the program for maintainability and extendability reasons. Or the fact that the first day is 1.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.