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

In summary: Also, seatChoice isn't bad, but it might be better as specifiedSeat or requestedSeat.The Java convention is to put the opening brace ({) for a method or loop or if on the same line as the method/loop/if, but the closing brace should be on a line by itself.Your code has another problem, which is that it doesn't work, at least not for seat choices with non-zero row numbers. I don't want to give it away, but the problem is in the else if statement. It's close, but not quite right.In summary, the conversation discusses a code that is not working properly. The code is supposed to reserve a seat in a plane, but it outputs a line saying that the seat
  • #1
major_maths
30
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
  • #2
I am not sure but ASCII code for zero is 48 and not 49.
 
  • #3
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.
 
  • #4
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.
 
  • #5


I would suggest reviewing the logic in your checkAvail method and checking for any potential errors or mistakes in your code. It seems that the program is not correctly identifying the availability of seats and is allowing reservations to be made even when the seat is already taken. Additionally, it may be helpful to use a debugger or print statements to track the values of your variables and see where the issue may be occurring. It is also important to thoroughly test your code with different inputs to ensure that it is functioning correctly.
 

What is Java: Airplane Seating Chart?

Java: Airplane Seating Chart is a computer program written in the Java programming language that helps visualize and organize airplane seating arrangements for flight bookings.

How does Java: Airplane Seating Chart work?

Java: Airplane Seating Chart uses algorithms and data structures to map out the available seats on an airplane and assign seats to passengers based on their preferences and availability. It also allows for easy manipulation of the seating chart, such as changing seats or adding additional seats.

What are the benefits of using Java: Airplane Seating Chart?

Java: Airplane Seating Chart offers many benefits, such as improving the efficiency of seat assignments, reducing human error, and providing a visual representation of the seating chart for easy understanding. It also allows for easy customization and integration with other airline systems.

Is Java: Airplane Seating Chart used by all airlines?

No, Java: Airplane Seating Chart is a program that can be used by any airline that chooses to implement it. Some airlines may have their own proprietary systems or may use different programming languages.

Can Java: Airplane Seating Chart handle multiple flights and aircrafts?

Yes, Java: Airplane Seating Chart is designed to handle multiple flights and aircrafts. It can store data for different flights and aircrafts and can be easily modified to accommodate various seating configurations.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top