Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I'm not good in Java, currently learning it in school.
I'm here for some help, i did some code but have errors.

I want to create a boolean 2-D Array for theaterclass, and this is my code:
Java
import java.util.Scanner;
public class MovieHallOne
{
  public static void main(String[] args)
  {
      Scanner scanStr = new Scanner(System.in);
      boolean[][] seat = new boolean[5][5];
      seat[0][0] = true;
      System.out.println("X = Seat occupied"+"\n\n"+"   <<--------- SCREEN ---------- >>");
      for(int i = 0; i < 5; i++)
      {
          for(int j = 0; j < 5; j++)
          {
              String seating =(seat[i][j]==true)?"   [X] ":"   [ ] ";
              System.out.print(seating);
          }
          System.out.println("");
      }
      System.out.print("\nPlease choose a seat:");
      String chooseSeat = scanStr.nextLine();
  }
}

So far, the result of my codes look like this :
http://i187.photobucket.com/albums/x163/DarkChorus/TheaterHallOne.jpg[^]

The problem is I want the user to choose the seat, thus having the layout of the cinema hall to change as in :

Before user selects seat :
<br />
 X = Seat occupied<br />
	  <<------------ SCREEN ------------ >>   <br />
	A1[ ]  A2[ ]   A3[ ]  A4[ ]  A5[ ]  A6[ ]<br />
	B1[ ]  B2[ ]   B3[ ]  B4[ ]  B5[ ]  B6[ ]<br />
	C1[ ]  C2[ ]   C3[ ]  C4[ ]  C5[ ]  C6[ ]<br />
	D1[ ]  D2[ ]   D3[ ]  D4[ ]  D5[ ]  D6[ ]<br />
	E1[ ]  E2[ ]   E3[ ]  E4[ ]  E5[ ]  E6[ ]<br />
<br />
Please choose your seat:<br />

======
And after user selects the seat, "A3" and "A4", the X will mark the square brackets showing the seat's occupied:

<br />
 X = Seat occupied<br />
	  <<------------ SCREEN ------------ >>   <br />
	A1[ ]  A2[ ]   A3[X]  A4[X]  A5[ ]  A6[ ]<br />
	B1[ ]  B2[ ]   B3[ ]  B4[ ]  B5[ ]  B6[ ]<br />
	C1[ ]  C2[ ]   C3[ ]  C4[ ]  C5[ ]  C6[ ]<br />
	D1[ ]  D2[ ]   D3[ ]  D4[ ]  D5[ ]  D6[ ]<br />
	E1[ ]  E2[ ]   E3[ ]  E4[ ]  E5[ ]  E6[ ]<br />
<br />
Please choose your seat:


However my code does not print like this, I have linked an image above, showing that there's no "A1" and so on in front of the "[ ]" ... and how do I enable user to select the seat and it will automatically "store" the "X" into the "[ ]"?

And this is just Hall 1, and there are 2 more halls with more seats, so if i get this hall right, I'll be fine with the rest of the halls.

I'm using really simple methods and have not touched on GUI stuffs, so do not try telling me about those stuffs or I wouldn't be able to understand, thanks in advance!

Any ideas, solutions, improvement I should make as I'm at my wits' end?

:(( :confused: :((
Posted
Updated 24-Jul-10 8:31am
v2
Comments
Sandeep Mewara 24-Jul-10 14:31pm    
Reason for my vote of 5
1. OP has tried
2. Nicely framed question
TheyCallMeMrJames 24-Jul-10 18:02pm    
Reason for my vote of 5
Good effort and well-written request.

You look like you've got your thinking cap on here, so I am not going to post code (I also don't currently have a Java compiler/IDE installed), but I will give you a bread crumb trail to follow. You should make out okay, but if you're looking for more code don't be afraid to ask.

You seem like you can think/code your way through this.

You will have to pseudo-generate the letters in your sequences. For example, we know that the ASCII value[^] for capital 'A' is 65. You can therefore use (char)65 to print the letter out. If you add the value of i to this, you will then get your B and your C (etc.) on subsequent lines.

You should use String.format[^] to create the line. This will allow you to pre-compute the letter/number and insert it for each element of the array and, more importantly, keep your code easy to read. What I mean by this is that you should change the way you build your seating variable and have it output the correct letter and the number representing the element in the array that you are currently drawing.

Finally, on the input side, you'll want to do a few things:
1) Verify that the input in two chars with the string length
2) Check the first character, then minus 65. That is the 'row' that you will store the value in.
3) Check the second character, and extract the value as an integer using Integer.parseint[^]. This value will be your 'column'.

With that information, you will have the data you need to flip the boolean value on the correct element in your array.

Good luck!
 
Share this answer
 
v2
Is this the way to do it?

Are there any examples on how to use the(char)65?


Quote : You will have to pseudo-generate the letters in your sequences. For example, we know that the ASCII value[^] for capital 'A' is 65. You can therefore use (char)65 to print the letter out. If you add the value of i to this, you will then get your B and your C (etc.) on subsequent lines.

String Alpha[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

      <big>
      int i = (char)65;
      int j = (char)65;
      </big>
      System.out.println("X = Seat occupied"+"\n\n"+"   <<--------- SCREEN ---------- >>");
      for(i = 0; i < 5; i++)
      {
          for(j = 0; j < 5; j++)
          {
             String seating =(seat[i][j]==true)?"   [X] ":"   [ ] ";
             System.out.print(seating);
          }
          System.out.println("");
      }
      System.out.print("\nPlease choose a seat:");
      String chooseSeat = scanStr.nextLine();



And about using String.format, is is something like this:

String format = "what to place here..."
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");
      System.out.format(format, "[ ]", "[ ]", "[ ]","[ ]", "[ ]");


Any examples or codes to change the way I build my seating variable and have it print the correct letter and the number representing the element in the array that I am currently drawing?

Sorry if I misunderstood or not really understood your tips ..

Just need more "bread crumbs" or clues.
:confused:
 
Share this answer
 
Do not use integer values to represent characters as it makes your code less clear, you can do a simple loop like this:
for (char row = 'A'; row < 'C'; row++) {
    for (int col = 1; col < 4; col++) {
    System.out.format("%c%d[ ] ", row, col);
    }
    System.out.println("");
}

The addition of code to check whether to print the 'X' should not be too difficult. You can use an expression like
int index = row - 'A';
to find the row index into your array.

Also remember when the user enters a seat identifier that the input will most likely use lower case characters so you should check for character 'a' as well as 'A'.
 
Share this answer
 
C#
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seatingplan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Sunshine
 */
public class SeatingPlan {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        boolean[][] seat = new boolean[10][20];
        char[] alphabets=new char[]{'A','B','C','D','E','F','G','H','I','J'};
        System.out.println("X = Seat occupied" + "\n\n" + "   <<--------- SCREEN ---------- >>");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 20; j++) {
                String seating = (seat[i][j] == true) ? alphabets[i]+""+j+"   [X] " : alphabets[i]+""+j+ "   [0] ";
                System.out.print(seating);
            }
            System.out.println("");
        }
        System.out.println("Please select a seat for your self");
        BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
        String bookthisSeat=br1.readLine().toUpperCase();
        char a=bookthisSeat.charAt(0);

        int row=(int)a-65;
        int col= Integer.parseInt(bookthisSeat.substring(1,2));
        if (seat[row][col]==false)
        {
            seat[row][col]=true;
            System.out.println("Congrats!! your ticket is booked and seat is now reserved for you");

        }
        else
            System.out.println("sorry your ticket can not be booked this seat is already occupied.... Please fined another seat");

    }
}
 
Share this answer
 
Comments
SoMad 27-Jan-13 17:39pm    
Don't wake up questions that are this old. I am not downvoting you, but please don't do it again.

Soren Madsen
Ravi_Kiran_ 23-Dec-13 12:09pm    
okay ... but why not ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900