0

Why am I getting an error in both of these methods that say 'this method must return type string...when I am returning type string. I am trying to do the stupid ninety-nine bottles of beer on the wall question that I'm sure everyone had learning java. But in my book instead of it outputting the numbers (ie.99) it has to print out the words(ninety-nine). So I tried to break it down in these two methods, but it is saying that it needs to return a string and they all are. I even had them all bracketed out, but nothing changed.

public String rounds()
{
    if(beer>89)
        return "Ninety";
    else if(beer>79)
        return "Eighty";
    else if(beer>69)
        return "Seventy";
    else if (beer>59)
        return("Sixty");
    else if (beer>49)
        return ("Fifty");
    else if (beer>39)
        return("Forty");
    else if(beer>29)
        return("Thirty");
    else if(beer>19)
        return("Twenty");
    else if(beer==19)
        return "Nineteen";
    else if(beer==18)
        return "Eighteen";
    else if (beer==17)
        return("Seventeen");
    else if (beer==16)
        return ("Sixteen");
    else if (beer==15)
        return("Fifteen");
    else if(beer==14)
        return("Fourteen");
    else if(beer==13)
        return("Thirteen");
    else if(beer==12)
        return("Twelve");
    else if(beer==11)
        return("Eleven");
    else if(beer==10)
        return("Ten");
    }

private String ones()
{
    if(beer % 10==9)
        return("-Nine");
    else if(beer % 10==8)
        return("-Eight");
    else if(beer % 10==7)
        return("-Seven");
    else if(beer % 10==6)
        return("-Six");
    else if(beer % 10==5)
        return("-Five");
    else if(beer % 10==4)
        return("-Four");
    else if(beer % 10==3)
        return("-Three");
    else if(beer % 10==2)
        return("-Two");
    else if(beer % 10==1)
        return("-One"); 
    else if(beer % 10==0)
        return("Zero");
    }

}
Alicia
  • 53
  • 5

3 Answers3

1

First of all, I would use a switch statement for your case, like it is found in here.

To answer your question, the error is stemming from the fact that you do not have a return for the case when no condition is met. If you add a return with your desired output before the final closing bracket "}", that would solve your problem.

Luke Bajada
  • 1,737
  • 14
  • 17
  • oo I hadn't thought of that. so adding a default return should solve the issue? – Alicia Jun 19 '15 at 21:58
  • if I do a switch statement thought, I would have to type out 1-99, there is definitely an easier way – Alicia Jun 19 '15 at 22:07
  • well if you're printing out a different String according to the number you have to write all the cases. Otherwise do a for loop which prints out the actual number instead and do your logic in there – Luke Bajada Jun 20 '15 at 02:51
1

Because your "if-else" block is not covering every condition. Think about what will the method "rounds" return if "beer" value equals to '9'? You have to add an "else" block at the and like this one;

    else                        // you have to cover all conditions
        return "none";          // thus you should add these

Complete solution is as below;

public class TestReturn {

    public static void main(String[] args) {
        int beer = 69;
        System.out.println( beer + " : " + rounds(beer) + ones(beer));

    }

    public static String rounds(int beer) // beer must be declared either as a parameter or as a local variable
    {
        if (beer > 89)
            return "Ninety";
        else if (beer > 79)
            return "Eighty";
        else if (beer > 69)
            return "Seventy";
        else if (beer > 59)
            return ("Sixty");
        else if (beer > 49)
            return ("Fifty");
        else if (beer > 39)
            return ("Forty");
        else if (beer > 29)
            return ("Thirty");
        else if (beer > 19)
            return ("Twenty");
        else if (beer == 19)
            return "Nineteen";
        else if (beer == 18)
            return "Eighteen";
        else if (beer == 17)
            return ("Seventeen");
        else if (beer == 16)
            return ("Sixteen");
        else if (beer == 15)
            return ("Fifteen");
        else if (beer == 14)
            return ("Fourteen");
        else if (beer == 13)
            return ("Thirteen");
        else if (beer == 12)
            return ("Twelve");
        else if (beer == 11)
            return ("Eleven");
        else if (beer == 10)
            return ("Ten");
        else                        // you have to cover all conditions
            return "none";          // thus you should add these
    }

    private static String ones(int beer) // beer must be declared either as a parameter or as a local variable
    {
        if (beer % 10 == 9)
            return ("-Nine");
        else if (beer % 10 == 8)
            return ("-Eight");
        else if (beer % 10 == 7)
            return ("-Seven");
        else if (beer % 10 == 6)
            return ("-Six");
        else if (beer % 10 == 5)
            return ("-Five");
        else if (beer % 10 == 4)
            return ("-Four");
        else if (beer % 10 == 3)
            return ("-Three");
        else if (beer % 10 == 2)
            return ("-Two");
        else if (beer % 10 == 1)
            return ("-One");
        else if (beer % 10 == 0)
            return ("Zero");
        else                        // you have to cover all conditions
            return "none";          // thus you should add these
    }
}

And here is the output;

69 : Sixty-Nine
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
  • thanks! that worked for this issue. now it is printing out seventeen-seven, so now I'm dealing with that. One error down, one error to go – Alicia Jun 19 '15 at 22:27
0

Assign your string to a variable and return it. In the first case it could be no condition is met, which would cause no value to be returned.

This would work :

public String rounds()
{
   String res;
   if(beer>89)
     res = "Ninety";
   // ...
   else if(beer==10)
     res = "Ten";

return res;

}
Kevin
  • 2,813
  • 3
  • 20
  • 30