Java Why is My Java Airplane Seating Chart Program Not Working Properly?

  • Thread starter Thread starter major_maths
  • Start date Start date
  • Tags Tags
    Airplane Java
AI Thread Summary
The discussion revolves around issues with a seat reservation system's `checkAvail` method. The primary problem is that when attempting to reserve certain seats, the system incorrectly outputs a message indicating that the seat is unavailable, even though the reservation can still be made. This occurs particularly when reserving seat 6A after successfully reserving 4A. The output message appears twice when trying to reserve a seat on the same line as the initial choice. Participants suggest that the issue may stem from the use of the ASCII value for '0' being incorrectly referenced as 49 instead of 48. They recommend avoiding "magic numbers" in the code and instead using character constants for clarity. Additionally, suggestions are made to improve variable naming for better readability, advocating for names that clearly describe their purpose, such as changing 'x' to 'seatGrid'. The importance of proper code formatting for readability is also highlighted.
major_maths
Messages
30
Reaction score
0
I've been working on this and I can't figure out why, exactly, it's not working properly. I can reserve a seat 4A but when I try to reserve seat 6A, the output includes a line of string that says "The seat you requested is unavailable. Please make another selection." yet it still let's me make the reservation. Also, when I try to make a reservation on the same line as the initial choice, it shows that string again, but it's shown twice. I know it has something to do with the checkAvail method shown below, but I can't narrow it down anymore than that.



public static void checkAvail(String seatChoice, char[][] x)
{
char seatColumn=seatChoice.charAt(0);
char seatRow=seatChoice.charAt(1);

for(int y=0; y<x.length; y++)
{
for(int z=0; z<x[y].length; z++)
{
if (x[y][z]=='X')
{
System.out.println("The seat you requested is unavailable. Please make another selection.");
}
else if ((((int)seatColumn-49)==y)&&(seatRow==((int)seatRow+z))&&(x[y][z]!='X'))
{
System.out.println("Your seat, "+seatChoice+" has been reserved.");
x[y][z]='X';
}
else if (seatColumn=='X')
{
System.out.println("Thank you for flying Java Airlines. Please exit the program.");
System.exit(0);
}
}
}

}
 
Last edited:
Technology news on Phys.org
I am not sure but ASCII code for zero is 48 and not 49.
 
I'm not sure what is going on here unless I ran the code (and I don't feel like doing that!) but I think "49" is the wrong number.

I think you want to ASCII code for A (or possibly one less than the code for A).

The best way to get the value you want convert the character constant 'A' (or whatever) into an integer, not to write "magic numbers" like 49 which don't give the reader any clue what they are supposed to mean.
 
major_maths said:
I've been working on this and I can't figure out why, exactly, it's not working properly. I can reserve a seat 4A but when I try to reserve seat 6A, the output includes a line of string that says "The seat you requested is unavailable. Please make another selection." yet it still let's me make the reservation. Also, when I try to make a reservation on the same line as the initial choice, it shows that string again, but it's shown twice. I know it has something to do with the checkAvail method shown below, but I can't narrow it down anymore than that.



public static void checkAvail(String seatChoice, char[][] x)
{
char seatColumn=seatChoice.charAt(0);
char seatRow=seatChoice.charAt(1);

for(int y=0; y<x.length; y++)
{
for(int z=0; z<x[y].length; z++)
{
if (x[y][z]=='X')
{
System.out.println("The seat you requested is unavailable. Please make another selection.");
}
else if ((((int)seatColumn-49)==y)&&(seatRow==((int)seatRow+z))&&(x[y][z]!='X'))
{
System.out.println("Your seat, "+seatChoice+" has been reserved.");
x[y][z]='X';
}
else if (seatColumn=='X')
{
System.out.println("Thank you for flying Java Airlines. Please exit the program.");
System.exit(0);
}
}
}

}

I'm hopeful that stallionX and fleer answered your question.

However, for future reference, when you post code, but (code) and (/code) tags around it (but using brackets [] instead of parentheses). If you do this, your formatting will be preserved, and your code will be easier to read.
Code:
public static void checkAvail(String seatChoice, char[][] x)
{
    char seatColumn=seatChoice.charAt(0);
    char seatRow=seatChoice.charAt(1);
   
    for(int y=0; y<x.length; y++)
    {
        for(int z=0; z<x[y].length; z++)
        {
            if (x[y][z]=='X')
            {
                System.out.println("The seat you requested is unavailable. Please make another selection.");
            }
            else if ((((int)seatColumn-49)==y)&&(seatRow==((int)seatRow+z))&&(x[y][z]!='X'))
            {
                System.out.println("Your seat, "+seatChoice+" has been reserved.");
                x[y][z]='X';
            }
            else if (seatColumn=='X')
            {
                System.out.println("Thank you for flying Java Airlines. Please exit the program.");
                System.exit(0);
            }
        }
    }
}
Most of your variable names are reasonable and descriptive, but your array parameter should have a better name than x. It's usually OK for loop control variables to have single letter names (such as i and j), but other variables should have a name that suggests what they contain. A better name than x might be seatGrid.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top