0

This program takes in 10 temperatures from user input, computes the average, and then prints out the temps above average.

For some reason that I don't see, when I give the input of three temperatures (100, 90, 70 in that order) I get this output:

100 is an above average temperature.

100 is an above average temperature.

90 is an above average temperature.

But when I give this input (70, 90, 100 in that order), I get

70 is not an above average temperature.

70 is an above average temperature.

90 is an above average temperature.

70 is an above average temperature.

90 is an above average temperature.

70 is not an above average temperature.

java.lang.ArrayIndexOutOfBoundsException: 2
    at Lab9.main(Lab9.java:64)

Can someone help me see what I do not?

import java.util.Scanner;


public class Lab9 
{
    public static int[] temps, aboveAvgTemps;
    public static int temp, totalTemp, avgTemp, currentTemp, oldTemp, numAboveAvg, 
        numOfEntries;

    public static void main(String[] args) 
    {
        System.out.println("Welcome to the above average temperature tester program.");
        System.out.println("Please in an integer for the number of days you wish to measure.");

        Scanner kb = new Scanner(System.in);

        numOfEntries = kb.nextInt();

        System.out.println("Please enter in " + numOfEntries + " temperatures.");

        //initialize main array and variables
        temps = new int[numOfEntries];
        totalTemp = 0;


        for(int i = 0; i < temps.length; i++)
        {
            System.out.println("Please enter in the temperature for day " + (i+1) + ":");
            temp = kb.nextInt();
            temps[i] = temp;
             totalTemp += temp;
        }       

        //final calculations
        avgTemp = totalTemp/numOfEntries;
        System.out.println("The average temperature is " + avgTemp);

        //count up num of above avg temps
        for(int i = 0; i < temps.length; i++)
        {
            if(temps[i] > avgTemp)
            {               
                numAboveAvg++;
            }
        }

        //initialize new array and variables for above avg temps
        aboveAvgTemps = new int[numAboveAvg];

        for(int i = 0; i < temps.length; i++)
        {
            if(temps[i] > avgTemp)
            {       
                for(int j = 0; j <= i; j++)
                {                   
                    aboveAvgTemps[j] = temps[j];
                    System.out.println(aboveAvgTemps[j] + " is an above average temperature.");
                }
            }
            else
            {
                System.out.println(temps[i] + " is not an above average temperature.");
            }
        }
    }
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • Welcome to StackOverflow. Have you stepped through your code in the debugger, one line at a time? That will help you find the problem more quickly than asking here, and is what you should do _before_ posting here. Please visit the [help] and read [ask] to learn how to use this site effectively. – Jim Garrison Feb 15 '17 at 23:14
  • You shouldn't do a loop over `j` inside of your loop over `i`. – matt Feb 15 '17 at 23:19
  • You should also reconsider your data types. What if the user enters 2 and 3? That would give an average of 2 with your solution, instead of 2.5. See http://stackoverflow.com/questions/7220681/division-of-integers-in-java – Philipp Merkle Feb 15 '17 at 23:26

6 Answers6

0

I don't what you are trying to do with aboveAvgTemps array. The for loop inside this snippet

if(temps[i] > avgTemp)
            {       
                for(int j = 0; j <= i; j++)
                {                   
                    aboveAvgTemps[j] = temps[j];
                    System.out.println(aboveAvgTemps[j] + " is an above average temperature.");
                }
            }

is causing the problem. If you remove the for loop and just print "xx is above avg temp.", you will get the desired result.

daBigBug
  • 343
  • 2
  • 11
0

I have changed the last block of code :

//initialize new array and variables for above avg temps
        aboveAvgTemps = new int[numAboveAvg];

        int index = 0;
        for(int i = 0; i < temps.length; i++)
        {
            if(temps[i] > avgTemp)
            {
                    aboveAvgTemps[index] = temps[i];

                    System.out.println(aboveAvgTemps[index] + " is an above average temperature.");

                    index++;
            }
            else
            {
                System.out.println(temps[i] + " is not an above average temperature.");
            }
        }
Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37
0

Your for loop with the "J" variable executes for upto i times when 1 above average temp is found. This leads to multiple prints and an out of bounds exception. The last loop should be

for(int i = 0; i < temps.length; i++)
    {
        if(temps[i] > avgTemp)
        {
            System.out.println(temps[i] + " is an above average temperature.");
        }
        else
        {
            System.out.println(temps[i] + " is not an above average temperature.");
        }
    }
user1200501
  • 123
  • 1
  • 5
0

The problem comes from the last block, I've changed it like this:

    int j = 0;
    for (int i = 0; i < temps.length; i++) {
        if (temps[i] > avgTemp) {
            aboveAvgTemps[j] = temps[i];
            System.out.println(aboveAvgTemps[j] + " is an above average temperature.");
            j++;
        } else {
            System.out.println(temps[i] + " is not an above average temperature.");
        }
    }
QVaucher
  • 40
  • 5
0

This is obviously an assignment. When I was in school doing these, the first thing we always had to do and what was worth 50% of our grade, was pseudo code for our solution. Even before that though I think it is very important for new programmers to think about the problem that they are trying to solve before they ever lay hands to a keyboard. From what I gather the assignment is to:

  1. have the user enter some temperatures
  2. find the average temperature
  3. print out, for each temperature given, if it is above or below the average

You have 1 and 2 down; the problem you are having is with 3. If you just state what exactly you want to do for 3 before even coding I think a good description would be:

for every temperature the user entered, print out 'the temperature is above the average' if it is above the average, otherwise print out 'the temperature is below the average'

With that in mind it is very easy to convert that into code:

for(int i=0; i < i < temps.length; i++ ) { //for each temperature the user entered
    if(temps[i] > avgTemp){//if the temperature is above the average
        //print out the above average message
        System.out.println(temps[i] + " is an above average temperature.");

    }else{//otherwise if the temperature is below the average
        //print out the below average message
        System.out.println(temps[i] + " is a below average temperature.");
    }
}
Hangman4358
  • 411
  • 1
  • 4
  • 11
0
function myFunction() {
//array
var temp = [temp[1], temp[2], temp[3]];
//number of elements
var t_num = temp.length;
//sum of elements
var t_sum = [t1, t2, t3].reduce(function(){return a + b;}, 0);
//avarage of elements
var t_avrg = t_sum / t_num;
//alert if > avarage
for (var i=0; i < t_num; i++){
    if (temp[x] > t_avrg){
        alert(temp[x]);
    };
};
};
Leo Tahk
  • 420
  • 1
  • 6
  • 15