I am doing a program in which there are 8 planes seats the user gets to choose whgich seat they want.
I created the GUI buttons and what not (fairly easy), but I wanted to make an array....I'm rambling here, because I can't do this program the way I want.

okay here it is

String Nums []={"1","2","3"};

there is my array, so lets say the user selects seat 2 which would be Nums[1], how would I restrict seat 2 from being selected again, and if all the seats are selected, how would I tell the user all the seats are full.

Why not use the array as a sort of existence table. For example, you would have an array of boolean designating the number of seats:

boolean occupied[] = new boolean[N];

Now for each index i, i + 1 is the seat number. When the seat is occupied, set occupied to true. Then if the user tries to choose an occupied seat, all you need to do is test for true.

There are quite a few ways to test if all of the seats are taken, but by far the easiest is to maintain a counter that counts the number of occupied seats. If the counter is equal to the size of the array, the plane is full.

I would definately take Narue's suggestion of usign a boolean array, but if you are also familiar with multi dimensional arrays you could also take a but further and try and do this:

int numOfColumns = 40;
int numOfRows = 8;
boolean[][] seatTaken = new boolean[numOfColumns][numOfRows];

so now seatTaken[4][5] would represent the seat in column 4, row 5 of the plane, etc, etc.... :cheesy:

>so now seatTaken[4][5] would represent the seat in column 4, row 5 of the plane
Column 5, row 6 of the plane. Because airlines don't use zero-based indexing, there's a difference between the pysical rows and columns and the logical rows and columns. It's best to avoid fencepost errors, right? ;)

Can you tell me why I get an error within my actionPerformed method please

public class ActionExample extends Applet implements ActionListener
{
Button okButton1;
Button okButton2;
Button okButton3;
Button okButton4;
Button okButton5;
// Smoking buttons
Button sokButton1;
Button sokButton2;
Button sokButton3;
Button sokButton4;
Button sokButton5;


Label remarkLabel;
Label remark2Label;
TextField answer;
public void init()
{


String Nums[]={"1","2","3","4","5","6","7","8","9","10"};



setLayout(new FlowLayout());
remarkLabel = new Label("                             ");
remark2Label = new Label("                            ");
answer=  new TextField(5);
okButton1 = new Button(Nums[0]);
okButton2 = new Button(Nums[1]);
okButton3 = new Button(Nums[2]);
okButton4 = new Button(Nums[3]);
okButton5 = new Button(Nums[4]);


sokButton1 = new Button(Nums[5]);
sokButton2 = new Button(Nums[6]);
sokButton3 = new Button(Nums[7]);
sokButton4 = new Button(Nums[8]);
sokButton5 = new Button(Nums[9]);


// always says where the component should be placed when adding
// Options are center,East,West,North and South


add(remarkLabel);
add(remark2Label);
add(answer);
add(okButton1);
add(okButton2);
add(okButton3);
add(okButton4);
add(okButton5);
add(sokButton1);
add(sokButton2);
add(sokButton3);
add(sokButton4);
add(sokButton5);


remarkLabel.setText("Please type 1 for Non Smoking");
remark2Label.setText("Please type 2 for Smoking");


okButton1.addActionListener(this);
okButton2.addActionListener(this);


}


public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==okButton1)
answer.setText("You have" +Nums[0]);



}



}

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.