Results 1 to 6 of 6
  1. #1
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Simplifying airplane seating project

    My main goal for this program is to make a 2d array with 7 rows and 4 columns of seating (A,B,C,D). The user inputs what row then what seat and the program marks and x when its occupied and gives an error if you try to get a seat already assigned then closes once all seats are taken. I have completed all expectation but now I just dont know how to trim the array instead of having to manually copy and paste all the info for each of the 7 rows which is a complete eyesore. Can anyone give me some tips on how I can get this done.
    Java Code:
    /**
     * Created by Dmp on 9/15/2016.
     */
    public class Airplane {
        public Airplane() {
        }
    
        public static void main(String[] args) {
            Boolean quit = Boolean.FALSE;
            Scanner input = new Scanner(System.in);
            Scanner input2 = new Scanner(System.in);
            String[][] seats = new String[7][4];
            seats[0][0] = "A";
            seats[0][1] = "B";
            seats[0][2] = "C";
            seats[0][3] = "D";
            seats[1][0] = "A";
            seats[1][1] = "B";
            seats[1][2] = "C";
            seats[1][3] = "D";
            seats[2][0] = "A";
            seats[2][1] = "B";
            seats[2][2] = "C";
            seats[2][3] = "D";
            seats[3][0] = "A";
            seats[3][1] = "B";
            seats[3][2] = "C";
            seats[3][3] = "D";
            seats[4][0] = "A";
            seats[4][1] = "B";
            seats[4][2] = "C";
            seats[4][3] = "D";
            seats[5][0] = "A";
            seats[5][1] = "B";
            seats[5][2] = "C";
            seats[5][3] = "D";
            seats[6][0] = "A";
            seats[6][1] = "B";
            seats[6][2] = "C";
            seats[6][3] = "D";
    
    
            while (!quit) {
                int row;
                for (row = 0; row <= 6; ++row) {
                    System.out.print(row + 1);
    
                    for (int colum = 0; colum <= 3; ++colum) {
                        System.out.printf(seats[row][colum]);
                    }
    
                    System.out.print("\n");
                }
    
                System.out.print("Enter Row Number: ");
                row = input.nextInt();
                System.out.print("Enter Letter Seat: ");
                String var7 = input2.nextLine();
    
                if (var7.equalsIgnoreCase("A")) {
                    if (seats[1][0].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][0] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("B")) {
                    if (seats[1][1].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][1] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("C")) {
                    if (seats[1][2].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][2] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("D")) {
                    if (seats[1][3].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][3] = "X";
                    quit = false;
    
                }
                if (var7.equalsIgnoreCase("A")) {
                    if (seats[0][0].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][0] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("B")) {
                    if (seats[0][1].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][1] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("C")) {
                    if (seats[0][2].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][2] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("D")) {
                    if (seats[0][3].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][3] = "X";
                    quit = false;
    
                }
    
                if (var7.equalsIgnoreCase("A")) {
                    if (seats[2][0].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][0] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("B")) {
                    if (seats[2][1].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][1] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("C")) {
                    if (seats[2][2].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][2] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("D")) {
                    if (seats[2][3].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][3] = "X";
                    quit = false;
    
                }
                if (var7.equalsIgnoreCase("A")) {
                    if (seats[3][0].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][0] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("B")) {
                    if (seats[3][1].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][1] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("C")) {
                    if (seats[3][2].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][2] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("D")) {
                    if (seats[3][3].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][3] = "X";
                    quit = false;
    
                }
                if (var7.equalsIgnoreCase("A")) {
                    if (seats[4][0].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][0] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("B")) {
                    if (seats[4][1].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][1] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("C")) {
                    if (seats[4][2].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][2] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("D")) {
                    if (seats[4][3].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][3] = "X";
                    quit = false;
    
                }
                if (var7.equalsIgnoreCase("A")) {
                    if (seats[5][0].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][0] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("B")) {
                    if (seats[5][1].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][1] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("C")) {
                    if (seats[5][2].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][2] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("D")) {
                    if (seats[5][3].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][3] = "X";
                    quit = false;
    
                }
                if (var7.equalsIgnoreCase("A")) {
                    if (seats[6][0].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][0] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("B")) {
                    if (seats[6][1].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][1] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("C")) {
                    if (seats[6][2].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][2] = "X";
                    quit = false;
                }
    
                if (var7.equalsIgnoreCase("D")) {
                    if (seats[6][3].equalsIgnoreCase("X"))
                        System.out.println("Already taken");
                    seats[row - 1][3] = "X";
                    quit = false;
    
                }
    
    
    
                if (seats[0][0].equalsIgnoreCase("X") && seats[0][1].equalsIgnoreCase("X") && seats[0][2].equalsIgnoreCase("X") && seats[0][3].equalsIgnoreCase("X") && seats[1][0].equalsIgnoreCase("X") && seats[1][1].equalsIgnoreCase("X") && seats[1][2].equalsIgnoreCase("X") && seats[1][3].equalsIgnoreCase("X") && seats[2][0].equalsIgnoreCase("X") && seats[2][1].equalsIgnoreCase("X") && seats[2][2].equalsIgnoreCase("X") && seats[2][3].equalsIgnoreCase("X") && seats[4][0].equalsIgnoreCase("X") && seats[4][1].equalsIgnoreCase("X") && seats[4][2].equalsIgnoreCase("X") && seats[4][3].equalsIgnoreCase("X")&& seats[5][0].equalsIgnoreCase("X") && seats[5][1].equalsIgnoreCase("X") && seats[5][2].equalsIgnoreCase("X") && seats[5][3].equalsIgnoreCase("X") && seats[6][0].equalsIgnoreCase("X") && seats[6][1].equalsIgnoreCase("X") && seats[6][2].equalsIgnoreCase("X") && seats[6][3].equalsIgnoreCase("X") && seats[3][0].equalsIgnoreCase("X") && seats[3][1].equalsIgnoreCase("X") && seats[3][2].equalsIgnoreCase("X") && seats[3][3].equalsIgnoreCase("X")) {
                    quit = Boolean.TRUE;
                }
    
    
            }
        }}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Simplifying airplane seating project

    how to trim the array instead of having to manually copy and paste all the info for each of the 7 rows
    Can you explain what the problem is?
    If you want to load values in an array, it will take some assignment statements to do it. There is a quicker way than what you have coded:
    Java Code:
            String[][] twoDim = new String[][] {{"1A", "1B", "1C"}, {"2A", "2B", "2C"}};
    What is the code from lines 67 through line 256 supposed to be doing. With repetitive statements like that there must be a way for a loop to do it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Simplifying airplane seating project

    67-256 Just have the if statements to see if the seat is already taken or not, how can I go around with putting it in a loop, sorry this is literally my second project

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Simplifying airplane seating project

    There are lots of ways to do it. The simplest is to use nested for loops. You might also want to reduce all your seat assignment code
    to just a few statements. You just need to calculate an index based on the chosen seat and row. Take a look at String.index and you
    might be able to see how to do it.

    Also, a lesson on 2D arrays. They are just arrays of arrays. So you can manipulate them
    as 1D arrays.

    Java Code:
          String[][] strings = new String[][]{{"A","B"},{"C","D"}};
          String[] row1 = strings[1];
          System.out.println(Arrays.toString(row1));
          System.out.println("-----------------------");
          
          String[][] foo = new String[2][];
          foo[0] = new String[]{"Q","R"};
          foo[1] = new String[]{"S","T"};
          for (String[] f : foo) {
             System.out.println(Arrays.toString(f));
          }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Simplifying airplane seating project

    see if the seat is already taken or not
    Given the row and column of the seat, use those values directly to see if the seat is taken:
    Java Code:
     if(!seats[row][column].equals(Occupied))  {
      // seat is empty 
      seats[row][column] = Occupied;  // flag as occupied
     }else {
      // seat is occupied - give message or ???
     }
    The variable: Occupied could be defined by:
    Java Code:
      final String Occupied = "X";   // flag to indicate a seat is occupied
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Simplifying airplane seating project

    Maybe a boolean 2D array would easier here. That's assuming the purpose is only to determine taken/not taken.

    ... and yes, please use nested loops.
    Last edited by benji2505; 09-16-2016 at 05:21 AM.

Similar Threads

  1. Seating Plan/Chart
    By loosehead in forum New To Java
    Replies: 6
    Last Post: 09-16-2014, 10:22 PM
  2. Airplane Reservation again
    By ChristianPaul in forum New To Java
    Replies: 1
    Last Post: 03-05-2014, 03:16 AM
  3. Airplane Reservation
    By RiterHeng in forum New To Java
    Replies: 5
    Last Post: 04-20-2012, 12:21 PM
  4. Having trouble simplifying my answer
    By skoutzombie in forum New To Java
    Replies: 9
    Last Post: 10-24-2011, 04:39 AM
  5. Replies: 0
    Last Post: 07-23-2009, 06:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •