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
Click For 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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
73
Views
6K
  • · Replies 5 ·
Replies
5
Views
5K