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.
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