I am working on an assignment that is making a program that adds and subtracts passengers that are on a plane, displayed via a 2d array. I am trying to write code that will check if the seat a passenger requests is reserved or open. I made for loops that iterate through my 2d array that check if the elements of the 2d array have a matching element of an array of Passenger names I made. If there is a name at the seat that the passenger requested, the program should print Denied
; if the seat is open the program should print Confirmed
. Right now, my program does this "correctly," but displays Denied
or Confirmed
a bunch of times. I am not sure how to fix this. I suspect it is because my break;
statements only cut off the innermost loop that the statement belongs to.
Here is all of my code(Some parts aren't yet finished but that won't interfere with what I am doing):
import java.util.Scanner;
public class Plane {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
chartMaker();
} //ends main
public static void chartMaker() {
String operator = " ";
String Pname = " ";
String SeatNum = " ";
int runtime = 0;
int SeatsTaken = 0;
int TotalSeats = 48;
int SeatsAvailable = 0;
String[][] chart = new String[][]{
{"1", "2", "3", "x", "4", "5", "6"},
{"7", "8", "9", "x", "10", "11", "12"},
{"13", "14", "15", "x", "16", "17", "18"},
{"19", "20", "21", "x", "22", "23", "24"},
{"25", "26", "27", "x", "28", "29", "30"},
{"31", "32", "33", "x", "34", "35", "36"},
{"37", "38", "39", "x", "40", "41", "42"},
{"43", "44", "45", "x", "46", "47", "48"}
};
for(int i = 0; i < chart.length; i++) //starts printing of chart
{
for(int j = 0; j < chart[i].length; j++)
{
System.out.print(chart[i][j] + " ");
}
System.out.println();
} //ends printing of chart
System.out.println();
while(runtime < 48) {
System.out.print("Enter + to add a passenger, - to delete a passenger, or q to quit: ");
operator = sc.nextLine();
switch(operator) {
case "+":
System.out.print("Enter passenger name: ");
Pname = sc.nextLine();
System.out.print("Enter seat number: ");
SeatNum = sc.nextLine();
SeatChance(Pname, SeatNum, SeatsTaken, TotalSeats, SeatsAvailable);
SeatsTaken++;
for(int a = 0; a < chart.length; a++) //prints the updated chart
{
for(int b = 0; b < chart[a].length; b++)
{
if(SeatNum.contentEquals(chart[a][b]))
{
chart[a][b] = Pname;
System.out.print(chart[a][b] + " ");
}else {
System.out.print(chart[a][b] + " ");
}
}System.out.println();
} //ends printing the updated chart
System.out.println("Passenger: " + Pname);
System.out.println("Seat Number: " + SeatNum);
String[] Passengers = new String[SeatsTaken];
Passengers[SeatsTaken - 1] = Pname;
System.out.println("Passengers: ");
for(int j = 0; j < Passengers.length; j++)
{
System.out.println(Passengers[j]);
}
for(int x = 0; x < chart.length; x++) //checks if seat is taken
{
for(int c = 0; c < chart[x].length; c++)
{
for(int g = 0; g < Passengers.length; g++)
{
if(chart[x][c].contentEquals(Passengers[g]))
{
System.out.println("Denied");
break;
}else {
System.out.println("Confirmed");
break;
}
}
}
}
break;
case "-":
System.out.println("");
break;
case "q":
runtime = runtime + 48;
System.out.println("End of Program");
break;
} //ends switch statement
runtime++;
} //ends while loop for asking user
/*UpdateChart(chart, Pname, SeatNum);
*/
} //ends chartMaker
//seatChance is broken
public static void SeatChance(String Pname, String SeatNum, int SeatsTaken, int TotalSeats, int SeatsAvailable) {
double SeatChance = 0.0;
SeatsAvailable = TotalSeats - SeatsTaken;
SeatChance = (SeatsAvailable / TotalSeats) * 100;
System.out.println(Pname + " has a " + SeatChance + "% chance of getting the seat");
} //ends SeatChance
/*
public static void UpdateChart(String[][] chart, String Pname, String SeatNum) {
for(int a = 0; a < chart.length; a++)
{
for(int b = 0; b < chart[a].length; b++)
{
if(SeatNum.equals(chart[a][b]))
{
System.out.print(Pname);
}else {
System.out.print(chart[a][b]);
}
}
System.out.println();
}
} //ends UpdateChart
*/
} //ends Plane
Here is my code that needs fixed:
for(int x = 0; x < chart.length; x++) //checks if seat is taken
{
for(int c = 0; c < chart[x].length; c++)
{
for(int g = 0; g < Passengers.length; g++)
{
if(chart[x][c].contentEquals(Passengers[g]))
{
System.out.println("Denied");
break;
}else {
System.out.println("Confirmed");
break;
}
}
}
}
And here is my output:
Passenger: John
Seat Number: 14
Passengers:
John
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Denied
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
I am assuming that the random Denied
in the output is caused when my 2d array updates the values of the elements in the array, but I am not really sure.