lundi 28 septembre 2015

How to check if an array has enough positions to accept new values to the array in Java?

I'm creating a commercial airline flight program and I'm an amateur at Java (learning new things everyday). So, I have an array of strings called seats, each position in this array corresponds to an available seat on the flight. Initially these seats are all empty.

String[] seats = new String[8];

In the long-run, this program is going to be inserting values into this array and my question is how can I check if there are enough seats or positions in this array to accept new passengers to assign them seats? plus if there are no seats available, I want my program to display an error message and not assign seats to anyone in the group. Here is what I came up with so far,

for(int i=0; i< seats.length-1;i++) {
   if(seats[i] != null) {
     do something;
   } else {
       system.out.println("No seats available");
       (how do I not assign seats or accept more values to my array?)
   }
}

Do you think I am on the right track with this code? if not, corrections/contributions would be helpful. How do I instruct the program not to assign seats if there are no empty or available spots on the flight?

Aucun commentaire:

Enregistrer un commentaire