Hi can someone help with code? It compiles correctly and everything works except
ID: 3534992 • Letter: H
Question
Hi can someone help with code? It compiles correctly and everything works except for one thing. The output goes something like this:
Seating Chart:
A1 A2 A3 A4 A5 A6
B1 B2 B3 B4 B5 B6
C1 C2 C3 C4 C5 C6
D1 D2 D3 D4 D5 D6
E1 E2...
F1
G1
H1
I1
J1
"What seat would you like to reserve?"
A4
"Enter r to continue to reserve seats or q to quit and show seating chart:"
r
"What seat would you like to reserve?"
K2
"Enter r to continue to reserve seats or q to quit and show seating chart:"
q
As you can see, there's no K2 on the seating chart, it's supposed to display this (error not valid is supposed to come up whenever the user types in a seat that is not shown on the chart):
"Error not a valid seat, please enter a seat to reserve:"
Is it a mismatch error? Anyone please know how to correct this? Thanks
The code:
import java.util.Arrays;
import java.util.Scanner;
public class Reserve {
String[][] seatArray=new String[10][6];
char[] seatNumber={'A','B','C','D','E','F','G','H','I','J'};
static Scanner scan=new Scanner(System.in);
public static void main(String[] args) {
String selectedSeat;
String flag="";
Reserve r =new Reserve();
System.out.println("Seating Chart");
System.out.println("----------------------------------------------");
r.initializeSeats();
do {
System.out.println("What seat would you like to reserve ?");
selectedSeat=scan.nextLine();
int row=0;
for(int i=0;i<10;i++) {
if(r.seatNumber[i]==selectedSeat.charAt(0)) {
row=i;
break;
}
}
char a=selectedSeat.charAt(1);
int column=Character.getNumericValue(a)-1;
if(!r.seatArray[row][column].equals("X")) {
r.seatArray[row][column]="X";
}
else {
System.out.println("Error, Not a valid seat Please enter valid seat");
continue;
}
System.out.println("Enter r to reserve more seats or q to quit and view a chart:");
flag=scan.nextLine();
}while(!flag.equals("q"));
System.out.println("Reserved Seating Chart");
System.out.println("----------------------------------------------");
r.displayReservedChart();
}
public void initializeSeats() {
for(int i=0;i<10;i++) {
for(int j=0;j<6;j++) {
seatArray[i][j]=""+seatNumber[i]+""+(j+1);
System.out.print(seatArray[i][j]+" ");
}
System.out.println("");
}
}
public void displayReservedChart() {
for(int i=0;i<10;i++) {
for(int j=0;j<6;j++) {
System.out.print(seatArray[i][j]+" ");
}
System.out.println("");
}
}
}
Explanation / Answer
you did not account for row being invalid http://ideone.com/rqpwWr
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.