-4
private static final String string1= "food budget-$3.00 per day or $10.00 per week.";
private static final String string2 = "food budget 2 - $2.00 per day or $7.00 per week.";
private static final String string3 = "food budget 3 - $1.00 per day or $5.00 per week.";
private static final String string4 = "food budget 4 - $%4.2f per day or $%5.2f per week.";

private static String calcRateClass(int cat, String food)
{
  while(gender.equals("m"))
     if(cat > 32 && cat < 64)
        return string1;
  if(cat > 24 && cat < 31)
     return string3;
  if (cat > 65)
     return string4; 
  else
     while(gender.equals("f"))
        if(age > 29 && age < 63)
           return string1;
  if(age > 24 && age < 30)
     return string2;
  if (age > 62)
     return string4;
}

need some help returning a string if any of the conditions are met later in my main
when I call rateResult = calcCatClass(age, gender);

thanks

ichramm
  • 6,437
  • 19
  • 30
zr0
  • 13
  • 1
  • 6

3 Answers3

0

You need a default return value for this to compile. Right now, the final statement in your function is:

if (age > 62)
    return string4;

If you get to this point of the function, and age is <= 62, your function will reach the end without having a value to return.

You should add a default return to make this compile:

if (age > 62)
    return string4;

return "ERROR - no conditions met!";
jkinkead
  • 4,101
  • 21
  • 33
0

In your String calcRateClass(int cat, String food) method if all conditions are invalid nothing will return so change it as,

String result = "";
if(cat > 32 && cat < 64)
     result = string1;
if(cat > 24 && cat < 31)
     result = string3;
      .
      .

return result;
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

It is because you can reach the end of the method and do not find any return statement, but you specified that a String will be returned.

In this case, you can add a final return statement returning null.

arutaku
  • 5,937
  • 1
  • 24
  • 38