Question: The following program has 2 issues I need fixed 1. When the program asks the user if they want to reserve the seat y/n, if
The following program has 2 issues I need fixed
1. When the program asks the user if they want to reserve the seat y/n, if the user types anything besides y/n it does not throw an error message saying "Invalid iput. Please enter y/n."
2. If the user enters a seat above row 9 such as 10 A the out currently prints that invalid seat number twice then the error message stating "This is not a valid choice" etc. I want the program NOT to print the seat number in this case and ONLY print the error message.
import java.util.Scanner;
public class Seat { static char[][] seats = new char[9][4]; static String[][] userNames = new String[9][4];
public static void main(String[] args) { Scanner read = new Scanner(System.in); for (int i = 0; i < 9; ++i) { for (int j = 0; j < 4; ++j) { seats[i][j] = (char) (j + 'A'); } } printSeatChart();
while (!isFull()) { boolean validSeat = false; int row = -1, col = -1; String seatNumber = null; String ro=""; while (!validSeat) { System.out.print("Enter seat number: "); seatNumber = read.nextLine().toUpperCase(); //convert to upper case System.out.println(seatNumber); int length = seatNumber.length(); // find length of string if(length>2) { ro = ro + seatNumber.charAt(0); ro = ro + seatNumber.charAt(1); row = Integer.parseInt(ro); col = seatNumber.charAt(2) - 'A'; System.out.println(row); } else { row = seatNumber.charAt(0) - '0' - 1; col = seatNumber.charAt(1) - 'A'; } if(row >= 10 || col > 3) { System.out.println("That is not valid choice! Please enter a different seat."); }else if (seats[row][col] == 'X') { System.out.format("Seat number: %s cannot be reserved. Please select a different seat. ", seatNumber); } else validSeat = true; } System.out.format("Would you like to reserve %s? Y/N: ", seatNumber); char userChoice = read.nextLine().toLowerCase().charAt(0); if (userChoice == 'y') { System.out.print("Enter your name: "); String userName = read.nextLine(); userNames[row][col] = userName; seats[row][col] = 'X'; System.out.format("Reservation of seat number: %s to: %s was successful. ", seatNumber, userName); printSeatChart(); } }
System.out.println("We are sorry, there are no more seats available."); }
private static void printSeatChart() { for (int i = 0; i < 9; ++i) { System.out.format("%d %c %c %c %c ", (i + 1), seats[i][0], seats[i][1], seats[i][2], seats[i][3]); } }
private static boolean isFull() { for (int i = 0; i < 9; ++i) { for (int j = 0; j < 4; ++j) { if (seats[i][j] != 'X') return false; } } return true; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
