So for homework I have to write a program that prints out a table of airline seats denoted with - by default (i.e. to show that they are "open" and eligible for booking). Subsequently, it is supposed to prompt the user for a seat they would like to purchase & change the value of that particular spot in the array to a X.
This is what my code looks like right now - it prints out the table just fine, but when I try to change the location it gives me an error:
import java.util.*;
public class AirlineSeeting {
public static void main(String[]args) {
int row = 0;
int colum = 0;
System.out.println("Enter n:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[][] SeetingChart = new String[n][4];
for (int i = 1; i <= 4; i++) {
System.out.printf("\t%d\t", i);
}
System.out.println("");
for (int j = 1; j <= n; j++) {
System.out.printf("%d", j);
for (int k = 1; k <= 4; k++) {
System.out.print("\t-\t");
}
System.out.println("");
}
for( int i = 0 ; i < SeetingChart.length; i++){
System.out.println("What row would you like to sit in");
row = scanner.nextInt();
System.out.println("What colum would you like to sit in");
colum = scanner.nextInt();
if (SeetingChart[row][colum].equals("X")){
System.out.println("Please pick a seat that is avalable");
}
else if (SeetingChart[row][colum].equals("-")){
SeetingChart[row][colum] = "X";
}
for (int j = 1; j <= 4; j++) {
System.out.printf("\t%d", j);
}
System.out.println("");
for (int j = 1; j <= n; j++) {
System.out.printf("%d", j);
for (int k = 1; k <= 4; k++) {
System.out.print("\t-");
}
System.out.println("");
}
}
}
}
This is the output I am getting when executing the above code:
Enter n:
9
1 2 3 4
1 - - - -
2 - - - -
3 - - - -
4 - - - -
5 - - - -
6 - - - -
7 - - - -
8 - - - -
9 - - - -
What row would you like to sit in
2
What colum would you like to sit in
2
Exception in thread "main" java.lang.NullPointerException
at AirlineSeeting.main(AirlineSeeting.java:30)
Any help with this will be greatly appreciated! Thank you!