Results 1 to 3 of 3
  1. #1
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default Filling a multidimensional array, and then change it

    This is my current code:

    Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows:
    1 A B C D
    2 A B C D
    3 A B C D
    4 A B C D
    5 A B C D
    6 A B C D
    7 A B C D

    The program should display the seat pattern, with an 'X' marking the seats already assigned. For example, after seats 1A, 2B, and 4C are taken, the display should look like:
    1 X B C D
    2 A X C D
    3 A B C D
    4 A B X D
    5 A B C D
    6 A B C D
    7 A B C D

    After displaying the seats available, the program prompts for the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that that seat is occupied and ask for another choice.

    Java Code:
    import java.util.Scanner;
    
    
    /**
      Simple program for tracking airplane seats
     */
    public class Seats
    {
    
        public static void main(String[] args)
        {
            // Create an array of characters for the seats
            char[][] seats = new char[7][4];
            char filledseat = 'x';
    
            // Populate it
            for (int i = 0; i < 7; i++) {
                seats[i][0] = 'A';
                seats[i][1] = 'B';
                seats[i][2] = 'C';
                seats[i][3] = 'D';
            }
    
            Scanner keyboard = new Scanner(System.in);
    
            // Make a counter for the number of seats filled
            int filled = 0;
            int row;
            int column;
            int stillmore;
    
            printSeats(seats);
    
    		// Prompt for seat input, then loop until all seats are filled
    		System.out.println("Enter row number.");
    		row = (keyboard.nextInt()-1);
    		System.out.println("Enter column.");
    		column = (keyboard.nextInt()-1);
    		System.out.println("If you do not wish to enter a seat, please enter 99 to exit.");
    		System.out.println("If you wish to enter more seats, please enter 100.");
    		stillmore = keyboard.nextInt();
    
    		if (stillmore == 99)
            	{
            		System.out.println("You entered 99, which means you do not want anymore seats.");
            	}
    
    		do
    		{
            	for (filled = 0; filled < 28; filled++)
            	{
    				seats[row][column] = filledseat;
    
                	if (filled < 28)
                	{
            			System.out.println("If you do not wish to enter another seat, please enter 99 to exit.");
            			System.out.println("If you still want to enter more seats please enter 100.");
            			stillmore = keyboard.nextInt();
    
            			if (stillmore == 99)
            				{
            					System.out.println("You entered 99, which means you do not want anymore seats.");
            				}
    
    					System.out.println("Enter row number.");
    					row = (keyboard.nextInt()-1);
    					System.out.println("Enter column.");
    					column = (keyboard.nextInt()-1);
                	}
            	}
    
        	} while (stillmore == 100);
    
        	System.out.println("Final seat assignments: ");
            		printSeats(seats);
        }
    
    
        /**
          Utility method that prints the contents of the seats array
         */
        private static void printSeats(char[][] seats)
        {
            for (int i = 0; i < seats.length; i++)
            {
                System.out.println((i + 1) + "   " +
                    seats[i][0] + " " + seats[i][1] + "  " +
                    seats[i][2] + " " + seats[i][3]);
            }
        }
    
    }
    now my problem is as follows: There are no error with the actual code, but there are logic errors, because the code does not do what I want it to. so, I enter several numbers for the seat location, and when I enter 99, all the program does is output ("You entered 99, which means you do not want anymore seats.") And then the program continues to ask for seat row and column. So I need help with the controling loop, so it exits the loop when the user enters 99.
    Thanks
    When in doubt, use a lightsaber

  2. #2
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    I see a problem in this part of your code:
    Java Code:
                	if (filled < 28)
                	{
            			System.out.println("If you do not wish to enter another seat, please enter 99 to exit.");
            			System.out.println("If you still want to enter more seats please enter 100.");
            			stillmore = keyboard.nextInt();
    
            			if (stillmore == 99)  //<<<<<<  PROBLEM
            				{
            					System.out.println("You entered 99, which means you do not want anymore seats.");
            				}
    
    					System.out.println("Enter row number.");
    					row = (keyboard.nextInt()-1);
    					System.out.println("Enter column.");
    					column = (keyboard.nextInt()-1);
                	}
    If the user enters 99, you print out that statement. However, you fall right through to the next statement and it prompts the user for another row number and column. I don't think you want to do that. Instead, add a break statement:
    Java Code:
                	if (filled < 28)
                	{
            			System.out.println("If you do not wish to enter another seat, please enter 99 to exit.");
            			System.out.println("If you still want to enter more seats please enter 100.");
            			stillmore = keyboard.nextInt();
    
            			if (stillmore == 99)  //<<<<<<  PROBLEM
            				{
            					System.out.println("You entered 99, which means you do not want anymore seats.");
                                                    break;  //<<<<<<<<<<<<<<<HERE
            				}
    
    					System.out.println("Enter row number.");
    					row = (keyboard.nextInt()-1);
    					System.out.println("Enter column.");
    					column = (keyboard.nextInt()-1);
                	}
    The break statement will cause and immediate exit of its surrounding loop (in this case, the for loop). The next thing that will happen is that the while loop condition (stillmore == 100) will be checked. I think that's the behavior you want. When the user enters 99, you are pretty much done with that for loop.

    This change will get you closer to what you're trying to accomplish. I see some duplicate prompting, though. Maybe you can clean that up. Good luck.

  3. #3
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Thank you for your help. The break statement now exits the for loop when 99 is entered. I also moved the several statements around which eliminated the same lines of code being outputed twice in a row.
    When in doubt, use a lightsaber

Similar Threads

  1. Help with filling an array
    By MaceMan in forum New To Java
    Replies: 6
    Last Post: 03-23-2011, 03:10 PM
  2. initialize multidimensional array
    By Aidoru in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 01:00 PM
  3. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 07:24 AM
  4. Navigate through a multidimensional array
    By VinTiger in forum New To Java
    Replies: 4
    Last Post: 05-05-2009, 07:16 PM
  5. [SOLVED] Multidimensional array
    By Torgero in forum New To Java
    Replies: 20
    Last Post: 03-22-2009, 11:36 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
  •