3

Basically I would like to store information about a train within arrays. I believe I would need to use a 2d array. I can't seem to get it working correctly. I want an array that will let me keep track of the number of empty and reserved seats on each car of a train. Is this going to work for me?

    int[][] seats = new int[4][10]; 
    for (int row = 0; row < 4; row ++)
        for (int col = 0; col < 10; col++)
            car[row][col] = 0;

I want for this just to initialize all the seats to 0, signifying they are empty. I currently get an error message on the semi colon after int[4][10], it says "expected , {"

4
  • 1
    @nAvEeDkHaN he's getting a compile error not a runtime one Commented Dec 12, 2013 at 19:36
  • The for statements should be in a code block Commented Dec 12, 2013 at 19:36
  • 1
    you're missing the brackets Commented Dec 12, 2013 at 19:37
  • It's less about arrays and more about your code formatting. It says it's expected a {, add some {'s to your code. Commented Dec 12, 2013 at 19:37

6 Answers 6

4

Your code is valid, the only error is that car should be seats:

int[][] seats = new int[4][10]; 
for (int row = 0; row < 4; row++)
   for (int col = 0; col < 10; col++)
      seats[row][col] = 0;
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe your IDE is misleading you, try to restart it (clearing the project might also help)
2

Well your first problem is that you have an array named "seats" but you are trying to initialize something called "car". Nesting for loops like that without including the '{', '}' to delimit them is, at least, bad style, and maybe has the compiler confused? The following works like a champ for me.

int[][] seats = new int[4][10];
for ( int rowIx = 0; rowIx < seats.length; rowIx++ ) {
    for ( int colIx = 0; colIx < seats[rowIx].length; colIx++ ) {
        seats[rowIx][colIx] = 0;
    }
}

...although it works equally well without the squigglies but I find that much harder to read. I also think you should get in the habit of asking the array its length rather than assuming fixed size. Thats what is going on in the seats.length and seats[rowIx].length calls.

Comments

1

Give this a try and see if it helps. I think the IDE is looking for initialization syntax since that's what you are doing in the loops. I could be mistaken on exactly what's going on but the code will compile for you.

int[][] seats = new int[4][10];
{
    for (int row = 0; row < 4; row++) {
        for (int col = 0; col < 10; col++) {
            seats[row][col] = 0;
        }
    }
}

Comments

0

Use a basic class to store the information.

class TrainCar
{
     private int _emptySeats;
     private int _totalSeats;

     public TrainCar( int total )
     {
         _totalSeats = total;
         _emptySeats = total;
     }

     public bool fillSeats( int num )
     {
         _emptySeats -= num;
         if( _emptySeats < 0 )
         {
             _emptySeats += num;
             return false;
         }
         return true;
     }

     public int getFilledSeats()
     {
         return _totalSeats - _emptySeats;
     }

     public int getEmptySeats()
     {
         return _emptySeats;
     }
}

And then just use an array (or ArrayList) of the TrainCar class

TrainCar[] cars = new TrainCar[ NUM_CARS ];
for( int i = 0; i < cars.length; ++i )
    cars[i] = new TrainCar( SEATS_PER_TRAINCAR );

1 Comment

I would do this, but this is for an Android app. I need the values to be passed and manipulated between different activities. From what I have learned today, SharedPreferences is the way for me to do this, but only allows me to use primitive data types.
0

In the fourth line, car needs to be replaced with seats. It is also good form to keep track of loops with { }, even for single lines.

int[][] seats = new int[4][10]; 
for (int row = 0; row < 4; row ++)
{
    for (int col = 0; col < 10; col++)
    {
        seats[row][col] = 0;
    }
}

Comments

-1

The syntax of the loop statement is not respected here : braces are needed !

tutorial explaining the syntax

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.