Skip to content
Advertisement

Java – Displaying the temperature statistics of the week using arrays

I am currently working on a problem set for an assignment at school, and I’m really close to finishing however I’m getting a few compilation errors.

The problem set includes displaying the weeks avg. temp, highest temp., lowest temp., and the days of the week that are hottest and coldest.

Currently what I’m trying to do is display the days of the week that are hottest, and if I work that out I can easily find the coldest days of the week.

I’m getting a few compilation errors when I try to compile the code which includes

  • incompatible types: int[] cannot be converted to int

  • error: cannot find symbol

It would be great if I could get some guidance on what to do, I’m currently at lost right now.

http://ideone.com/rOqV2Z

public class test1
{
// Main method
public static void main(String[] args)
{
    // Create a new scanner
    Scanner input = new Scanner(System.in);

    // Set array list
    int[] tempList = new int[7];

    // Prompt user for input and store input
    System.out.println("Enter the hightest temperature of each day for a week (starting on Sunday): ");
        for(int i = 0; i < tempList.length; i++)
            tempList[i] = input.nextInt();

    // Averages temperature - @@@@@@@ ASK WHY IT THERE ARE SO MANY DECIMALS ON THE SIDE WHEN AVERAGE ALL 1's
    double avgTemp = avgTemp(tempList);
        System.out.printf("The average temperature of the week is: %.2f degree %n", avgTemp);

    // Display hottest temperature
    int maxTemp = maxTemp(tempList);
        System.out.println("The highest temperature of the week is: " + maxTemp + " degree");

    // Display coldest temperature
    int minTemp = minTemp(tempList);
        System.out.println("The coldest temperature of the week is: " + minTemp + " degree");


    int[] maxTempList = searchTemp(tempList, maxTemp);

    for(int i = 0; i < maxTempList.length; i++){
        System.out.print("The hottest days of the week are: " +maxTempList[i]);

    System.out.print(weekDay(tempList,maxTemp));
    }
}

// Average the temperature
public static double avgTemp(int[] array)
{
    int tempTotal = array[0];

    // Total temperature values
    for(int i = 0; i < array.length; i++)
        tempTotal = array[i]+tempTotal;

    // Return temperature average.
    return ((double)tempTotal/array.length);
}

// Get hottest temperature
public static int maxTemp(int[] array)
{
    int max = array[0];

    // Check and replace max temp
    for(int i = 1; i < array.length; i++){
        if(max < array[i])
            max = array[i];

    }
    return max;
}

// Get coldest temperature
public static int minTemp(int[] array)
{
    int min = array[0];
    for(int i = 1; i < array.length; i++){
        if(min > array[i])
            min = array[i];
    }
    return min;
}


// Return days
public static String weekDay(int i, int[] array)
{
    int[] displayWeekDay = searchTemp(array, i);

    for(i = 0; i < displayWeekDay.length; i++){

        String weekDay = "";
        switch(i)
        {
            case 0: return "Sunday";
            case 1: return "Monday";
            case 2: return "Tuesday";
            case 3: return "Wednesdays";
            case 4: return "Thursday";
            case 5: return "Friday";
            case 6: return "Saturday";
        }
    }
    return weekDay;
}


// Finds the index of the hottest/coldest days
public static int[] searchTemp(int[] temp, int key)
{
    int count = 0;
    for(int i = 0; i < temp.length; i++){
        if(temp[i] == key)
            count++;
    }

    int[] index = new int[count];
    for(int j = 0; j < index.length; j++){
        for(int i = 0; i < temp.length; i++){
            if(temp[i] == key){
                if(j > 0 && index[j - 1] == i)
                    continue;
                else{
                    index[j] = i;
                    break;
                }
            }
        }
    }
    return index;
}

}

Advertisement

Answer

I went and checked the code using the website you linked.

  1. Firstly, you should learn to use the debugger, as it will usually tell you what the error is and where to find it.
Main.java:42: error: incompatible types: int[] cannot be converted to int
System.out.print(weekDay(tempList,maxTemp));
                             ^

Here it points to an error on the data type of tempList. It’s saying that an int array cannot be converted to an int. If you look at the weekDay() function you’ll see that the first argument is asking for an int, but you are passing an int array. It won’t work.

public static String weekDay(int i, int[] array)

EDIT: If you want to pass a specific value into the function from the array just use

System.out.print(weekDay(tempList[IntegerPosition],maxTemp));
                                        ^

  1. Main.java:104: error: cannot find symbol
    return weekDay;
           ^
    

This simply means it can’t find the variable in the current scope. There’s a lot to learn about this, but I’ll just get to the point.

    // Return days
    public static String weekDay(int i, int[] array)
    {
        int[] displayWeekDay = searchTemp(array, i);
        String weekDay = "";
        for(i = 0; i < displayWeekDay.length; i++){

            //String weekDay = ""; Declare weekDay outside of the loop
            switch(i)
            {
                //Assign a value to weekDay, simply returning won't do it
                case 0: weekDay = "Sunday"; break;
                case 1: weekDay = "Monday"; break;
                case 2: weekDay = "Tuesday"; break;
                case 3: weekDay = "Wednesdays"; break;
                case 4: weekDay = "Thursday"; break;
                case 5: weekDay = "Friday"; break;
                case 6: weekDay = "Saturday"; break;
            }
        }
        return weekDay;
    }

EDIT 2: As per the discussion, this is what I would do in order to be able to print multiple days that had the highest temperature

//Call the function directly without putting a print statement around it
weekDay(maxTemp,tempList)); 
//...
// Return days
public static void weekDay(int i, int[] array) //Change the return type to void
{
    int[] displayWeekDay = searchTemp(array, i);
    for(i = 0; i < displayWeekDay.length; i++){
        switch(displayWeekDay[i])
        {
            //Print each one
            case 0: System.out.println("Sunday"); break;
            case 1: System.out.println("Monday"); break;
            case 2: System.out.println("Tuesday"); break;
            case 3: System.out.println("Wednesday"); break;
            case 4: System.out.println("Thursday"); break;
            case 5: System.out.println("Friday"); break;
            case 6: System.out.println("Saturday"); break;
        }
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement