I'm writing a class to represent a flight. The only error I'm getting is on line 97.
Flight.java:97: error: missing return statement

/**
    Class Flight represents a plane at an airline

    Author: Anna Mae Decker
    E-mail address: adecker0033@kctcs.edu   
    Last changed: 02/10/13
    Assignment1
*/

public class Flight {

    /*
    * declare instance variables
    * each flight object that is created will have its own copy of these
    * These are declared as private to ensure that external Java code
    * cannot access them directly and assign any arbitrary values to them.
    */
    String flightNumber;
    int rows;
    int seatsPerRow;
    boolean [][] seat;
    double fare;
    String origin;
    String destination;




    /*
    * The constructor
    * Contains code that initially sets up each flight object when it is
    * first created. Always has the same name as the class.
    */
    public Flight (String flightNumber, int rows, int seatsPerRow, double fare, String origin, String destination) {
        //call the mutator methods and let them initialize the instance variables
        setFlightNumber(flightNumber);
        setRows(rows);
        setSeatsPerRow(seatsPerRow);
        setFare(fare);
        setOrigin(origin);
        setDestination(destination);

        seat = new boolean [rows][seatsPerRow];

        for(int i = rows; i>0; i--){
            for(int x = seatsPerRow; x>0; x--){
                seat[i][x] = true;
            }
        }

    }




    /*
    * The accessor methods
    * These report the current values of the Flight object's instance variables
    */
    public String getFlightNumber() {
        return flightNumber;
    }

    public int getRows() {
        return rows;
    }

    public int getSeatsPerRow() {
        return seatsPerRow;
    }

    public double getFare() {
        return fare;
    }

    public String getOrigin() {
        return origin;
    }

    public String getDestination() {
        return destination;
    }

    public boolean isAvailable(int userRow, int userSeatPerRow) {
      return !seat[userRow][userSeatPerRow];
    }/





//THIS METHOD IS THE ONE THAT IS CAUSING THE ERROR? AM I PLACING THE RETURN STATEMENT IN THE WRONG LOCATION?    
    public boolean anyAvailable(int userRow, int userSeatPerRow){
        for(int row = 0; row < getRows(); row++){
            for(int column = 0; column < getSeatsPerRow(); column++){
                if(!seat[userRow][userSeatPerRow]) {
                    return true;  //<<<<<HERE IS LINE 97
                }
            }
        }
    }





    /*
    * The mutator methods
    * These allow the Flight object's instance variables to be assigned new values
    */
    public void setFlightNumber(String flightNumber) {

        //trim the title parameter to remove any white space at the ends of the
        //String. Then test if the remaining String contains at least one 
        //character. If so, title is valid.
        if ((flightNumber.trim()).length() > 0) {
            this.flightNumber = flightNumber;
        }
    }


    public void setRows(int rows) {
        if (rows >= 0) {
            this.rows = rows;
        }
    }

    public void setSeatsPerRow(int seatsPerRow) {
        if (seatsPerRow >= 0) {
            this.seatsPerRow = seatsPerRow;
        }
    }

    public void setSeat(int rows, int seatsPerRow, boolean seat[][]) {
        if (rows >= 0) {
            if (seatsPerRow >=0) {
                seat = new boolean [rows][seatsPerRow];
            }
        }
    }   

    public void setFare(double fare) {
        if (fare >= 0) {
            this.fare = fare;
        }
    }

    public void setOrigin(String origin) {
        if ((origin.trim()).length() > 0) {
            this.origin = origin;
        }
    }   

    public void setDestination(String destination) {
        if ((destination.trim()).length() > 0) {
            this.destination = destination;
        }
    }   


}//end of class

Edited 3 Years Ago by adecker0033

You haven't told the compiler what to return if there are no seats available. Most people would guess that false should be returned, but the compiler needs that to be spelled out explicitly. It is complaining because if the outer loop finishes then it will run into the end of the method without having any value to return, which javac considers unacceptable.

csstu: next to the fact that you are just repeating what was already told, if it were to be implemented as you suggest, there would still be a compile time error and an added logical error.
1. compile time error: there still would be no return statement if there's a null send as param (for instance)
2. your logic would indicate that, if the first seat that is checked is taken, the method would return false, making it impossible to sell more than one ticket for a plane (bit expensive, not?)
the 'return false;' should be added after the ending of the for-loops, right before the closing bracket of the method.

just a thought for the OP: I assume this method is to be used right before adding a new 'passenger', that this method is just a verification whether or not a seat is available -
maybe you could opt to create a Class Seat, which has two instance members: row and seat (both int), and you return an instance of this class:

for instance:

public void isSeatAvailable(){
  Seat seat = null;
  // your for-loops here
  if(!seat[userRow][userSeatPerRow]) {
       seat = new Seat(userRow, userSeatPerRow);
       break;
  }
  // end of your for-loops
  return seat;
}

a possible advantage, say, you want to add a new Passenger, you can simply do something like this:

Seat addPosition = isSeatAvailable();
if ( addPosition != null ){
  seat[addPosition.getRow()][addPosition.getSeat()] = true; // you immediately have the position
}
else{
  // what to do if there's no free seat left.
} 
This article has been dead for over six months. Start a new discussion instead.