# loop for booking a seat for movies/cinema

Page 1 of 1

## 8 Replies - 9271 Views - Last Post: 29 January 2010 - 02:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=151881&amp;s=8810f6d2f6e43df626bbf79ccc3885e0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 amal hk

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 21-January 10

# loop for booking a seat for movies/cinema

Posted 25 January 2010 - 07:13 AM

i would like to know simpler explanation about this kind of loop..how the logic is...

the user will first display all the seats numbering, and then he'll enter which seats he want. here's the looping will do their job rite? then if their is a seat available, s.o.p. "Seats available", and it will again display the seats numbering with the one he just book, lets says cross out..

so, what i want is explanation regarding the loop...

Is This A Good Question/Topic? 0

## Replies To: loop for booking a seat for movies/cinema

### #2 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: loop for booking a seat for movies/cinema

Posted 25 January 2010 - 07:24 AM

Look at the steps that need to happen.
1) Display all seats' numbering (do you have a list of that already? Then you will need a for loop to go through the list)
``` for( int i = 0; i < someLists.length(); i ++ )
```

2) Prompt User for selection ( you might want a while loop to wait for valid input here)

```String input = "";
```

3) Modify the list w/ new value

4) do same loop as in step one

Now take those steps and try and write some code for each one. Post what you got

### #3 amal hk

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 21-January 10

## Re: loop for booking a seat for movies/cinema

Posted 28 January 2010 - 06:59 AM

there are mistakes for my code, plz help me,thanks

import java.io.*;

public class SeatsBooking {

public static void main(String args[]) throws Exception {

final int SIZE = 7;//my lecturer ask to declare first and not put number inside []
final int SIZE2 = 4;

String[][] seats = new String[SIZE][SIZE2];

for (int i = 0; i < 7; i++) {
seats[i][0] = "A";
seats[i][1] = "B";
seats[i][2] = "C";
seats[i][3] = "D";
}

while (true) {
System.out.println("1. Display");
System.out.println("2. Assign");
System.out.println("3. Exit");
System.out.println("Enter your choice : ");

String choice = br.readLine();

switch (Integer.parseInt(choice)) {
case 1:
display(seats);
break;

case 2:
assign(seats);
break;

case 3:
exit();
break;

default:
System.out.println("Invalid input");
}
}
}

public static void display(String seats[][]) {
for (int i = 0; i < 7; i++) {
System.out.print(i + 1);
for (int j = 0; j < 4; j++) {
System.out.print(" " + seats[i][j]);
}
System.out.println();
}
}

public static void assign(String seats[][]) throws Exception {

display(seats);

System.out.println("Enter row = ");
String row = br.readLine();
System.out.println("Enter seat (A, B, C, D) = ");
String col = br.readLine();

for (int i = 0; i < 4; i++) {

if (seats[Integer.parseInt(row) - 1][i].equalsIgnoreCase(col)) {
seats[Integer.parseInt(row) - 1][i] = "X";
}
}
}

public static void exit() {
System.exit(0);
}
}
```
```
[code]

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: loop for booking a seat for movies/cinema

Posted 28 January 2010 - 08:12 AM

Also, please specifically describe any errors you are encountering.

### #5 amal hk

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 21-January 10

## Re: loop for booking a seat for movies/cinema

Posted 28 January 2010 - 11:19 AM

btw this is the summary of the Q.

after displaying the seats available, the program should prompt for the seat desired, the user can type in a seat, and then the display of available seats should be updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that seats is occupied and ask for another choice.

errors i encounter:

it's only display the seats and user can enter the seat number...and the rest is not working...

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: loop for booking a seat for movies/cinema

Posted 28 January 2010 - 12:58 PM

Knowing what's wrong with your code doesn't help me if I can't read it.

### #7 amal hk

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 21-January 10

## Re: loop for booking a seat for movies/cinema

Posted 29 January 2010 - 04:46 AM

```
there are mistakes for my code, plz help me,thanks

import java.io.*;

public class SeatsBooking {

public static void main(String args[]) throws Exception {

final int SIZE = 7;//my lecturer ask to declare first and not put number inside []
final int SIZE2 = 4;

String[][] seats = new String[SIZE][SIZE2];

for (int i = 0; i < 7; i++) {
seats[i][0] = "A";
seats[i][1] = "B";
seats[i][2] = "C";
seats[i][3] = "D";
}

while (true) {
System.out.println("1. Display");
System.out.println("2. Assign");
System.out.println("3. Exit");
System.out.println("Enter your choice : ");

String choice = br.readLine();

switch (Integer.parseInt(choice)) {
case 1:
display(seats);
break;

case 2:
assign(seats);
break;

case 3:
exit();
break;

default:
System.out.println("Invalid input");
}
}
}

public static void display(String seats[][]) {
for (int i = 0; i < 7; i++) {
System.out.print(i + 1);
for (int j = 0; j < 4; j++) {
System.out.print(" " + seats[i][j]);
}
System.out.println();
}
}

public static void assign(String seats[][]) throws Exception {

display(seats);

System.out.println("Enter row = ");
String row = br.readLine();
System.out.println("Enter seat (A, B, C, D) = ");
String col = br.readLine();

for (int i = 0; i < 4; i++) {

if (seats[Integer.parseInt(row) - 1][i].equalsIgnoreCase(col)) {
seats[Integer.parseInt(row) - 1][i] = "X";
}
}
}

public static void exit() {
System.exit(0);
}
}
```

### #8 amal hk

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 21-January 10

## Re: loop for booking a seat for movies/cinema

Posted 29 January 2010 - 06:28 AM

the program's working, but if I'm assigning new seat, n seem to be that the seats marked as "X", but unfortunately it's still accept the new seats without prompt "invalid seats"

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: loop for booking a seat for movies/cinema

Posted 29 January 2010 - 02:25 PM

Could you please edit your last post with code to include proper indentations so that we can read it easier? That way, we'll be able to better help you debug.

I've gone ahead and indented this method gratis so that I can get you started with some debugging. Notice how it looks more organized and easier to follow than your above post with code. Anyways, your problem is that you simply don't include a prompt. Basically, you're going to want to loop until you get a piece of valid input. I've gone ahead and added a do-loop and a swtich statement inside your method to illustrate. Make sure to read over the comments I've made in the code:
```public static void assign(String seats[][]) throws Exception {

display(seats);

boolean validAssignment = false;
do{ //loops until you get valid input

//start by prompting for and collecting input
System.out.println("Enter row = ");
String row = br.readLine();
System.out.println("Enter seat (A, B, C, D) = ");
String col = br.readLine();
int rowNum = Integer.parseInt(row); //more efficient than constantly parsing String

switch(col.charAt(0)){ //get character for seat
case 'A': //if it is capital or lowercase A
case 'a':
if(seats[rowNum - 1][0].equals("X")) //check to see if seat is taken
System.out.println("Invalid choice"); //if it is, print invalid choice
else{ //otherwise, mark it as taken
seats[rowNum-1][0] = "X";
validAssignment = true; //flag that you have valid input
}
break;  //and break out the switch statement

//here, you need to fill in options for B-D in the same manner that I did for A
}//end switch

}while(!validAssignment);
}

```