Reputation: 11
I am making a seating chart program. It is mostly working apart from the first seat appearing on a different line and another issue which is the main one. The main issue is that when I try to enter multiple names in the seating chart, it only keeps one.
I know it has something to do with my variable "name". "Name" just keeps on being written over by the next name entered.
My code:
package programs;
import java.util.*;
public class SeatingChart {
java.util.Scanner scan = new java.util.Scanner(System.in);
boolean seating[] = new boolean[24];
boolean runAgain = true;
int input;
static String name;
//runs the program with four options
public void runProgram()
{
do{
System.out.println("");
System.out.println("Press 1 to change a seat, 2 to print the seating chart,"
+ " 3 to clear all the seats or 4 to exit the program");
input = scan.nextInt();
scan.nextLine();
switch(input)
{
case 1:
emptySeat();
break;
case 2:
seatingChart(seating);
break;
case 3:
clearSeats();
break;
case 4:
runAgain = false;
break;
default:
System.out.println("That is not an option, please try again!");
}
}while(runAgain);
}
//changes an empty seat to a student's name at any location
public void emptySeat()
{
System.out.println("Who will be taking this seat?");
name = scan.nextLine();
System.out.print("Which seat would you like (1-24)\n");
int seat = scan.nextInt();
if (seat > 0 && seat <= 24) {
if (seating[seat - 1]) {
System.out.print("That seat is taken.\n");
} else {
seating[seat - 1] = true;
System.out.print("Seat number " + seat + " was assigned.\n");
}
}
}
//replace an empty seat with a person in the seating chart
public static void seatingChart(boolean seat[]) {
for(int i = 0; i < seat.length; i++) {
if(seat[i]) {
System.out.print(name + " ");
} else {
System.out.print("o ");
}
if(i % 8 == 0) {
System.out.println();
}
}
}
//clears all the seats
public void clearSeats()
{
}
public static void main(String[] args)
{
SeatingChart prog = new SeatingChart();
prog.runProgram();
}
}
Current wrong output:
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Bob
Which seat would you like (1-24)
3
Seat number 3 was assigned.
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2
o
o Bob o o o o o o
o o o o o o o o
o o o o o o o
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Joe
Which seat would you like (1-24)
5
Seat number 5 was assigned.
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2
o
o Joe o Joe o o o o
o o o o o o o o
o o o o o o o
What I want the output to be:
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Bob
Which seat would you like (1-24)
3
Seat number 3 was assigned.
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2
o o Bob o o o o
o o o o o o o o
o o o o o o o o
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
1
Who will be taking this seat?
Joe
Which seat would you like (1-24)
5
Seat number 5 was assigned.
Press 1 to change a seat, 2 to print the seating chart, 3 to clear all the seats or 4 to exit the program
2
o o Bob o Joe o
o o o o o o o o
o o o o o o o o
Upvotes: 1
Views: 91
Reputation: 341
Instead of using an array of booleans to represent your seating chart, perhaps you could use an array of strings. If the seat is empty, you leave that string as null. If the seat is full, you set the string at that index equal to the name of the person sitting in it.
Upvotes: 2