-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";      
}
2
  • 3
    What should your method return if neither of those conditions is true? Commented Jul 13, 2019 at 5: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. Commented Jul 13, 2019 at 5:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This will result in an ArrayIndexOutOfBoundsException.
@AndyTurner Corrected

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.