My program keeps looping when asking for seat letter. Can anyone help me out as to why that is?

import java.io.*;
import java.util.*;

public class AirplaneSeating
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) throws IOException 
    {
    
        char[][] seatPlan = new char[13][6];
        char response;
    
        initializeSeatPlan(seatPlan);
    
        showMenu(seatPlan);
    
        System.out.print("\nWould you like to reserve a seat Y(Yes) or N(No): ");
        response = console.next().charAt(0);
        System.out.println();
    
        while(response == 'y' || response == 'Y')
        {
            assignSeat(seatPlan);
            showMenu(seatPlan);
            System.out.println("Reserve another seat Y(Yes) or N(No): ");
            response = console.next().charAt(0);
            System.out.println(); 
        }
    }
    
    public static void initializeSeatPlan(char[][] sPlan)
    {
        int i, j;

        for(i = 0; i < sPlan.length; i++)
            for(j = 0; j < sPlan[0].length; j++)
                sPlan[i][j] = '*';
    }

    public static void showSeatAssignments(char[][] sPlan)
    {
        int i, j;

        System.out.println("       A B C  D E F");

        for(i = 0; i < sPlan.length; i++)
        {
            if(i < 9)
                System.out.print("Row  " + (i+1) + " ");
            else
                System.out.print("Row " + (i+1) + " ");

                for(j = 0; j < sPlan[0].length; j++)
                {
                    System.out.print(sPlan[i][j] + " ");
                    
                    if(j == 2)
                        System.out.print(" ");
                }
                System.out.println();
        }

        System.out.println("* -- available seat");
        System.out.println("X -- occupied seat");
        System.out.println();
    }

    public static void assignSeat(char[][] sPlan) throws IOException
    {
        char ticketType;
        char response;

        System.out.print("Enter ticket type: F(first class) "
                + " E(Economy class): ");
        ticketType = console.next().charAt(0);
        System.out.println();

        switch(ticketType)
        {
        case 'f':
        case 'F': 
            if(!isFirstClassFull(sPlan))
                assignFirstClassSeat(sPlan);
            else
            {
                System.out.println("No first class seats are available");
                System.out.print("Press Y(Yes) to continue: ");
                response = console.next().charAt(0);
                System.out.println();
            }
            break;
        case 'e':
        case 'E': 
            if(!isNonSmokingEconomicFull(sPlan) ||!isSmokingEconomicFull(sPlan))
                assignEconomicSeat(sPlan);
            else
            {
                System.out.println("No economy class seats are available");
                System.out.print("Press Y(Yes) to continue: ");
                response = console.next().charAt(0);
                System.out.println();
            }
        }

        showSeatAssignments(sPlan);
    }

    public static void showMenu(char[][] sPlan)
    {
        System.out.println("The current seat assignments is as follows.");
        showSeatAssignments(sPlan);
        System.out.println("Rows 1 and 2 are for first class passengers");
        System.out.println("Rows 1 through 7 are nonsmoking");
    }

    public static boolean isFirstClassFull(char[][] sPlan)
    {
        int i, j;

        int assignedSeats = 0;

        for(i = 0; i < 2; i++)
            for(j = 0; j < sPlan[0].length; j++)
                if(sPlan[i][j] == 'X')
                    assignedSeats++;

        return (assignedSeats == 2 * sPlan[0].length);
    }

    public static boolean isNonSmokingEconomicFull(char[][] sPlan)
    {
        int i, j;

        int assignedSeats = 0;

        for(i = 2; i < 7; i++)
            for(j = 0; j < sPlan[0].length; j++)
                if(sPlan[i][j] == 'X')
                    assignedSeats++;

        return (assignedSeats == 5 * sPlan[0].length);
    }

    public static boolean isSmokingEconomicFull(char[][] sPlan)    
    {
        int i, j;
        
        int assignedSeats = 0;

        for(i = 7; i < 13; i++)
            for(j = 0; j < sPlan[0].length; j++)
                if(sPlan[i][j] == 'X')
                    assignedSeats++;

        return (assignedSeats == 6 * sPlan[0].length);
    }

    public static void selectSeatNumber(int startRows, int endRows, int rowNo, char columnNo) 
                                        throws FileNotFoundException, IOException
    {
            
        System.out.print("Enter Row number " + startRows + " - " + endRows + ": ");
        rowNo = console.nextInt();
        System.out.println();

            while(rowNo < startRows || rowNo > endRows)
            {
                System.out.print("Enter Row number " + startRows + " - " + endRows + ": ");
                rowNo = console.nextInt();
                System.out.println();
            }

        System.out.print("Enter seat letter (A - F): ");
        columnNo = console.next().charAt(0);
        System.out.println();

            while(columnNo <= 'A' || columnNo >= 'F')
            {
                System.out.print("Enter seat letter (A - F): ");
                columnNo = console.next().charAt(0);
                System.out.println();
            }
    }

    public static void assignFirstClassSeat(char[][] sPlan) throws FileNotFoundException, IOException
    {
        int rowNum = 0;
        char columnPos = 0;
                        
        if(!isFirstClassFull(sPlan))
        {
            selectSeatNumber(1, 2, rowNum, columnPos);

            while(sPlan[rowNum - 1][(int)columnPos - 65] != '*')
            {
                System.out.println("*This seat is occupied*");
                System.out.println("Make another selection");

                showSeatAssignments(sPlan);

                selectSeatNumber(1, 2, rowNum, columnPos);
            }

            sPlan[rowNum - 1][(int)columnPos - 65] = 'X';
            System.out.println("This seat is reserved for you");
        }
        else
            System.out.println("First Class is now full");
    }

    public static void assignEconomicSeat(char[][] sPlan) throws IOException
    {
        char seatType;

        if(!isNonSmokingEconomicFull(sPlan) && isSmokingEconomicFull(sPlan))
            assignSeatNonSmokingEconomic(sPlan);
        else 
            if(isNonSmokingEconomicFull(sPlan) && !isSmokingEconomicFull(sPlan))
                assignSeatSmokingEconomic(sPlan);
            else 
                if(!isNonSmokingEconomicFull(sPlan) && !isSmokingEconomicFull(sPlan))
                {
                    System.out.print("Enter seat type: S(Smoking)or N(nonsmoking):");
                    seatType = console.next().charAt(0);
                    System.out.println();

                    while(seatType != 'S' && seatType != 's' && seatType != 'N' && seatType != 'n')
                    {    
                        System.out.println("Invalid seat type.");
                        System.out.print("Enter S(Smoking)or N(nonsmoking): ");
                        seatType = console.next().charAt(0);
                        System.out.println();
                    }

                if(seatType == 'N' || seatType == 'n')
                    assignSeatNonSmokingEconomic(sPlan);
                else
                    assignSeatSmokingEconomic(sPlan);
            }
            else
                System.out.println("Economic Class is now Full");
    }

    public static void assignSeatNonSmokingEconomic(char[][] sPlan) throws IOException
    {
        int rowNum = 0;
        char columnPos = 0;

        if(!isNonSmokingEconomicFull(sPlan))
        {
            selectSeatNumber(3, 7, rowNum, columnPos);

            while(sPlan[rowNum - 1][(int)columnPos - 65] != '*')
            {
                System.out.println("*This seat is occupied *");
                System.out.println("Make another selection");

                showSeatAssignments(sPlan);

                selectSeatNumber(3, 7, rowNum, columnPos);
            }

            sPlan[rowNum - 1][(int)columnPos - 65] = 'X';
            System.out.println("This seat has been reserved for you");
        }
        else
            System.out.println("Non smoking section of economic class is Full");
    }

    public static void assignSeatSmokingEconomic(char[][] sPlan) throws IOException
    {
        int rowNum = 0;
        char columnPos = 0;
        
        if(!isSmokingEconomicFull(sPlan))
        {
            selectSeatNumber(8, 13, rowNum, columnPos);

            while(sPlan[rowNum - 1][(int)columnPos - 65] != '*')
            {
                System.out.println("* This seat is occupied *");
                System.out.println("Make another selection");

                showSeatAssignments(sPlan);

                selectSeatNumber(8, 13, rowNum, columnPos);
            }    

            sPlan[rowNum - 1][(int)columnPos - 65] = 'X';
            System.out.println("This seat is reserved for you");
        }
        else
            System.out.println("Smoking section of economic class is Full");
    }
}

Recommended Answers

All 8 Replies

your while loop for seats doesn't work properly you should not use mathematical comparison on characters or strings. You can use either every present equals() method or compareTo().
I used equals and little redo row section

//All the code till line 177
        while(!rowChecking(columnNo))
        {
            System.out.print("Enter seat letter (A - F): ");
            columnNo = console.next().charAt(0);
            System.out.println();
        }
}

private static boolean rowChecking(char columnNo)
{
    Character[] rowLett = {'A','B','C','D','E','F'};
    for(int i = 0; i < rowLett.length; i++)
    {
        if(rowLett[i].equals(columnNo))
            return true;
    }
    return false;
}

PS: You can add lower case character into array in rowChecking method so user is not limited only to upper case, other posibility would be using some ignore case method

PS2: Once your run it with this addition it will kick of with another error at sPlan[rowNum - 1][(int)columnPos - 65] = 'X'; in assignFirstClassSeat() method. This is because both values are zero and you try to subtract from them Array out of bondaries

can you explain to me why exactly the original code works when asking for the seat number and not the seat letter? kinda confused

Because number can be greater or smaller then other number, however you can not apply same logic to characters ( I think Perl is exception in this). As I said before you should use equal() or any of compareTo() when you work with characters or strings

Couldn't you also convert the char to an integer, then compare the two chars? Being careful, of course, for case considerations?

> Because number can be greater or smaller then other number,
> however you can not apply same logic to characters

Yes, you can, because characters form a part of numeric data type. There are only two primitive types in Java: boolean and numeric.

Directly comparing characters can be troublesome if your application is aimed at providing internationalization in which case some sort of unicode sensitive comparison needs to be provided.

> Couldn't you also convert the char to an integer, then compare
> the two chars?

Not applicable to the discussion here; read above.

> My program keeps looping when asking for seat letter. Can anyone
> help me out as to why that is?

As I see now, your program is a complete mess with the core logic tightly coupled with I/O and hard coded magic number everywhere. Try redesigning your application and assigning proper responsibilities to well thought out classes.

BASE.
I strongly recommend you think of categories of object-oriented language.
Create your own classes: Airplane and Seat Life will become easier.

- Whether teachers have forgotten about OOL/OOP?

- What do you think about this?

> Because number can be greater or smaller then other number,
> however you can not apply same logic to characters

Yes, you can, because characters form a part of numeric data type. There are only two primitive types in Java: boolean and numeric.

Directly comparing characters can be troublesome if your application is aimed at providing internationalization in which case some sort of unicode sensitive comparison needs to be provided.

One always learn something new, if he listen...
Thanx

Thanks for all the input guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.