Help. I've been tasked with writing a program that calculates the average temp for a week, getting user input for daily temps, calculating the average and printing out the results. I got that part.
The second part is asking me to print out a message (Too hot!) if the avg is over 100 and (Too cold!) if the average is below 0.
This is where I am stumbling over myself.

here's what I have written:

import java.util.Scanner;

public class AvgTemp
{
    public static void main (String args[])
    {
        int tempM, tempT, tempW, tempR, tempF;
        double average;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter temperature of Monday: ");
        tempM = input.nextInt();

        System.out.print("Enter temperature of Tuesday: ");
        tempT = input.nextInt();

        System.out.print("Enter temperature of Wednesday: ");
        tempW = input.nextInt();

        System.out.print("Enter temperature of Thursday: ");
        tempR = input.nextInt();

        System.out.print("Enter temperature of Friday: ");
        tempF = input.nextInt();

        average = calcTemp(tempM, tempT, tempW, tempR, tempF);

        System.out.println("The average temperature this week is " +average);

        System.exit(0);
    }

    public static double calcTemp (int tempM, int tempT, int tempW, int tempR, int tempF)

    {
        double averageTemp;

        averageTemp = (tempM + tempT + tempW + tempR + tempF)/5.0;

        return averageTemp;

        if (averageTemp > 100);
            System.out.println("Too hot!");

        if (averageTemp < 0);
            System.out.println("Too cold!");        

        System.exit(0);
    }
}

As it stands, I am getting an unreachablestatment error in my first "if" statement and a missing return statement error at the end of the program and I cannot understand why.

Recommended Answers

All 7 Replies

public static double calcTemp

here, the return type is double, so you either have to return a double, or change the return type to void.

at the end of your method, there is a System.exit(0); which will automatically end your application. since you call this method, and still have statements behind that call, how would the application be able to continue further?

Even after you fix that...
When your method reaches the return statement it will immediatley return to the caller, and any remaining statements will not be executed. That's what your error message means - lines 43-49 can never be reached because line 41 will exit the method.

commented: ah, missed that :) +13

Any statement After return will not execute .
You Will have to shift those statementS which are below return to main program
at line30

 public void calcTemp (int tempM, int tempT, int tempW, int tempR, int tempF)
{
double averageTemp;
averageTemp = (tempM + tempT + tempW + tempR + tempF)/5.0;
return averageTemp;
if (averageTemp >= 100);
System.out.println("Too hot!");
if (averageTemp <= 0);
System.out.println("Too cold!");

}
}

I don't see why you need to return anything, use this.

commented: Wrong, ignores answers, too late -3

Hello Michael: You may not see why, but the compiler does not agree with you. Did you not understand the previous posts, or were you too busy to read them? I've entered you for the "worst post of 2015" contest.

there should be no semi colon after the if expression. just after the statements. check that too.

if(expression)
statement;


//not this way:

if(expression);
statement;

just take the if statements from the function and let it return the average. the u can use the average in your main function in which ever way u want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.