Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java: Airplane Seating Chart

  1. Sep 20, 2011 #1
    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 lets 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: Sep 20, 2011
  2. jcsd
  3. Sep 20, 2011 #2
    I am not sure but ASCII code for zero is 48 and not 49.
     
  4. Sep 20, 2011 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  5. Sep 20, 2011 #4

    Mark44

    Staff: Mentor

    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 (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java: Airplane Seating Chart
  1. Java Resources (Replies: 4)

  2. [Java] PNGException (Replies: 2)

  3. JAVA GridLayout (Replies: 1)

  4. Java interpreter (Replies: 8)

  5. Java Cylinders (Replies: 2)

Loading...