Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a return Statement? - new to coding

Tags:

java

bluej

The idea is different lots in an auction. I know I need a return statement of type "Lot" however I am not sure what that would be. Here is my code.

public Lot getLot(int lotNumber)
{
    int index = 0;
    boolean found = false;

    while(index < lots.size() && !found) {
        Lot selectedLot = lots.get(index);
        if(selectedLot.getNumber() == lotNumber) {
            found = true;

        }
        else {
            index++;

        }

    }

    if(!found) {
        found = false;

    }

}

1 Answers

What you could do is something similar to this, what this will do is if a match is found it will return the selectedLot which was the match, and if no match was found it would return null:

public Lot getLot(int lotNumber) {
    int index = 0;

    while(index < lots.size()) {
        Lot selectedLot = lots.get(index);

        if (selectedLot.getNumber() == lotNumber) {
            return selectedLot;
        } else {
            index++;
        }
    }

    return null;
}
like image 191
maldahleh Avatar answered Nov 23 '25 18:11

maldahleh