Alex
Alex

Reputation: 79

Seat Locator - 3D Arrays (Java)

I basically got the 3 dimensional array to display, but now I am having trouble getting the user input to correspond to the program and find the seat (replacing the specific 0 to a 1). Could someone Please give me a hint of what I am suppose to do? Here is my code.

import java.util.Scanner;
public class TicketSeller {

//name

public static void main(String[] args) {

     Scanner keyboard = new Scanner(System.in);
    int seating[][][] = new int[3][10][10];
    int g,r,c;

    System.out.print("Enter the ticket section: ");
    g = keyboard.nextInt();

    System.out.print("Enter the ticket row: ");
    r = keyboard.nextInt();

    System.out.print("Enter the ticket number: ");
    c = keyboard.nextInt();


    for( g = 0; g<3; g++)
        for(r=0; r<10; r++){
            for(c=0; c<10; c++)
                seating[g][r][c]= g*r*c;
    {
        for( g = 0; g<3; g++)
            for(r=0; r<10; r++){
                for(c=0; c<10; c++)

    {   

        System.out.print(seating[g][r][c]);     
    }
        System.out.println();
    }
        System.out.println();
    }
        }
}}

And output:

Enter the ticket section: 1

Enter the ticket row: 1 

Enter the ticket number: 1 

0000000000 

0000000000

0000000000

Upvotes: 0

Views: 187

Answers (2)

Prettygeek
Prettygeek

Reputation: 2531

You can get rid of 3D array with offsets, then you have one dimension and you calculate position. You know how many seats are in row and how many rows are in section. Nested loops are undebugable. Consider using calculated offsets. Or wriite some container, or class to hold data.

Class Section to hold section data

Class Row to hold row data

Class Seat to hold seat data (then you can adress more states than two)

And section wil hold several rows, each row will hold some seats. Then you can ask section to give you information of some seat. Like Fasade pattern. Java is objective language, make advantage of it.

Upvotes: 0

RealSkeptic
RealSkeptic

Reputation: 34618

First, you should use your IDE and format the code properly. Proper indentation is very important for understanding some of the problems in your code.

Problem 1

You are using the same variable names again and again. As soon as you use the variables g,r and c as loop variables, you erase their previous values, which were what the user entered. You should use different variables for the loops.

Problem 2

You have two nested loops (for g and for r). Inside the for r you have a loop that fills the seatings with values - all the c cells for the current g and r. Since they are all 0 at the first round, this just fills 0 in the seatings.

Then, after the seatings are filled, you have again loops with the same loop variables. The loops do what they need to do - they print the seatings. But the seatings at this stage are all zero. And that's the output you get.

But because you used the same loop variables, the variables c,r and g have reached their maximum values (3,10,10). This means the outer loop will not be performed again.

Never use loops inside loops with the same variables.

Problem 3

This problem is related to the previous problem. And this is where proper indentation and formatting would have helped you. This part:

for( g = 0; g<3; g++)
    for(r=0; r<10; r++){
        for(c=0; c<10; c++)
            seating[g][r][c]= g*r*c;
{

Both the { here should not be here. The first one, right after the for r causes the second set of loops to be included in the first. The second one is not actually creating a loop block. You probably thought you were closing your block, but you were actually opening a new block.

These are your loops with proper formatting:

    for (g = 0; g < 3; g++)
        for (r = 0; r < 10; r++) {
        ┆   for (c = 0; c < 10; c++)
        ┆       seating[g][r][c] = g * r * c;
        ┆   {
        ┆   ╻  for (g = 0; g < 3; g++)
        ┆   ╻       for (r = 0; r < 10; r++) {
        ┆   ╻           for (c = 0; c < 10; c++) {
        ┆   ╻               System.out.print(seating[g][r][c]);
        ┆   ╻           }
        ┆   ╻           System.out.println();
        ┆   ╻       }
        ┆   ╻   System.out.println();
        ┆   }
        }

The part that I marked with ┈┈┈ is all inside the for r loop. The part that I marked with ╺╺╺╺ is just an independent block and it's actually under the for r, not the for c loop, because it's not connected to the () of a for.

The solution to this is to always put { after the for(). If you always put { after for, while, if and else, even if there is only one line, you'll avoid a lot of troubles because the compiler will tell you if you have a missing brace, and also if you decide to add something to one of the loops, you won't get commands that you thought were inside your loop or if and are actually outside it.

Problem 4

There is no reason to put g*r*c inside the seats. You wanted to put 1 in the appropriate seat, do just that.

In conclusion:

  • Don't use the same variable names for the user inputs and the loop variables. Call the user variables userSection, userRow, userNumber and the loop variables section, row and number for example.
  • Take care to have a { after each for() and that you put the } in the proper place.
  • You didn't actually put a 1 in there anywhere.

Upvotes: 1

Related Questions