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

  • Context: Java 
  • Thread starter Thread starter major_maths
  • Start date Start date
  • Tags Tags
    Airplane Java
Click For Summary

Discussion Overview

The discussion revolves around a Java program designed to manage an airplane seating chart, specifically addressing issues related to seat reservation functionality. Participants explore potential problems in the code, particularly focusing on the method responsible for checking seat availability.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an issue where reserving seat 6A incorrectly outputs an unavailability message while still allowing the reservation to proceed, indicating a potential flaw in the checkAvail method.
  • Another participant suggests that the ASCII code for zero is 48, implying that the use of 49 in the code may be incorrect.
  • A different participant proposes that 49 might not be the correct number and suggests using the ASCII code for 'A' or a value one less than that, advocating against the use of "magic numbers" for clarity.
  • One participant reiterates the original problem with the checkAvail method, providing the same code snippet and expressing hope that previous responses addressed the issue.
  • Suggestions are made regarding code formatting for future posts, emphasizing the use of code tags for better readability.
  • Another participant comments on variable naming, recommending that the array parameter should have a more descriptive name than 'x', suggesting 'seatGrid' as an alternative.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the ASCII values used in the code, and there is no consensus on the exact cause of the issues described. The discussion remains unresolved regarding the specific errors in the code.

Contextual Notes

Limitations include potential misunderstandings of ASCII values, the use of unclear variable names, and the lack of clarity in the logic of the checkAvail method. These factors contribute to the ongoing uncertainty about the program's functionality.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
3K
  • · 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