-2

I had problem returning the string although I've placed both return statements inside the if else condition.

public String getAvailableRooms(int numofGuest)
{
    int i;

    for (i=0; i<rooms.length; i++)
    {
        if(rooms[i].getstatusRoom()==false)
            rooms[i].getnumofBeds();
    }
    String temp =  "The available room is room " + i + "and the number of beds " + rooms[i].getnumofBeds();
    if (rooms[i].getnumofBeds() >= numofGuest)
        return temp;
    else if (numofGuest> 4)
        return "Theres no available room";      
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Ming
  • 1
  • 3
    What should your method return if neither of those conditions is true? – khelwood Jul 13 '19 at 05:51
  • 1
    Also the logic in your code does not make sense. Inside your loop you're calling `getnumofBeds` but not doing anything with the result. Then after the loop, `i` will be just past the end of your array, but you're still trying to use it as an array index. – khelwood Jul 13 '19 at 05:58

1 Answers1

0

You are not returning anything if both of your if conditions are false i.e. you are missing and else statement. Try the following, it should work for you.(because there are only 2 conditions)

if (some_condition)
    return temp;
else
    return "Theres no available room"; 

If the above doesn't work, try putting an else statement in your if condition.

Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86