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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 7K views
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:
Physics 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.