0

I am stuck in an infinite for-loop and cannot figure out why. I set the bounds of the nested for loops to the entered rows and columns but still goes on infinitely. Heres my Plane class and my Main Class:

Plane:

public class Plane {
private Passenger[][] pArray;
private String flightNumber;
private int rows;
private int seatsPerRow;

public Plane(String fn, int r, int spr) {
    flightNumber = fn;
    rows = r;
    seatsPerRow = spr;
    pArray = new Passenger[r][spr];
}

public boolean addPassenger(String name, int ro, int sir) {
    boolean result = false;

    if(ro <= rows && sir <= seatsPerRow && pArray[ro][sir] == null){
        pArray[ro][sir] = new Passenger(name);
        System.out.println("Passenger " + name + " was added.");
        result = true;
    }
    return result;
}

public boolean removePassenger(int r, int s){
    boolean result = false;

    if(pArray[r][s] != null){
        pArray[r][s] = null;
        System.out.println("Passenger was removed.");
        result = true;
    } else {
        System.out.println("Invalid seat -- please try again.");
    }
    return result;

}

public void showSeats(){
    System.out.print(" ");
    for (int j = 0; j < pArray[0].length; j++) {
        System.out.printf("|%25d|", j);
    }
    System.out.println();
    // Display the contents of the machine
    for (int i = 0; i < rows; i++) {
        // Display the row label
        System.out.printf("%2d", i);
        for (int j = 0; j < seatsPerRow; j++) {
            if (pArray[i][j] == null) {
                // slot is empty
                System.out.printf("|%25s|", "");
            } else {
                System.out.printf("|%25s|", pArray[i][j].getName());
            }
        }
        System.out.println();
    }
    System.out.println();
}

public void passengerList(){
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < seatsPerRow; j++){
            if(pArray[i][j] != null){
                System.out.println(pArray[i][j]);
            }
        }
    }
}

Main:

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String flight;
    int rows;
    int numRows;
    String line;

    System.out.println("Welcome to Oswego Airlines\nEnter a flgiht number: ");
    flight = sc.nextLine();     
    System.out.println("Enter the number of rows: ");
    line = sc.nextLine();
    rows = Integer.parseInt(line);
    System.out.println("Enter the number of seats per row: ");
    line = sc.nextLine();
    numRows = Integer.parseInt(line);

    Plane plane = new Plane(flight, rows, numRows);

    System.out.println("Enter add, remove, seats, list, or quit: ");
    String command = sc.nextLine();

    while(command != "quit"){
        if(command.equalsIgnoreCase("add")){
            System.out.println("Enter passenger name, row, and seat: ");
            String pName = sc.nextLine();
            String[] passenger = pName.split(" ");
            int rowParse = Integer.parseInt(passenger[1]);
            int seatParse = Integer.parseInt(passenger[2]);

            plane.addPassenger(passenger[0], rowParse, seatParse);

            System.out.println("Enter add, remove, seats, list, or quit: ");
            command = sc.nextLine();

        } else if (command.equalsIgnoreCase("remove")){
            System.out.println("Enter row and seat: ");

            String rName = sc.nextLine();
            String[] removal = rName.split(" ");
            int rowParse = Integer.parseInt(removal[0]);
            int seatParse = Integer.parseInt(removal[1]);

            plane.removePassenger(rowParse, seatParse);

            System.out.println("Enter add, remove, seats, list, or quit: ");
            command = sc.nextLine();

        } else if (command.equalsIgnoreCase("seats")){
            plane.showSeats();

            System.out.println("Enter add, remove, seats, list, or quit: ");
            command = sc.nextLine();
        } else if (command.equalsIgnoreCase("list")){
            plane.passengerList();
        } else {
            System.out.println("Unknown command -- please try again.\nEnter add, remove, seats, list, or quit: ");
            command = sc.nextLine();
        }
    }

    System.out.println("Closing Oswego Airlines");








}

}

My friend and I have looked at it but we can’t seem to figure it out.

1 Answers1

2

String equality needs to be checked using .equals(). In other words,

while(command != "quit"){

should be

while(!command.equals("quit")){

The former checks if they are literally the same object, rather than that they have equal values.

FatalError
  • 52,695
  • 14
  • 99
  • 116
  • 1
    or probably while(!"quit".equals(command)){ to avaoid null pointer exp :-) – Albert Pinto Oct 21 '15 at 22:21
  • @AlbertPinto: Agree that is safer in general. Though, in the case, it won't be `null`. – FatalError Oct 21 '15 at 22:23
  • 1
    @AlbertPinto You should use this only if you like to hide your programming mistakes in a failed equal check, which will be harder to find if your program behaves unexpected. – Tom Oct 21 '15 at 22:30