Cannot utilize an array properly

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 4
Reputation: SureStoreX is an unknown quantity at this point 
Solved Threads: 0
SureStoreX SureStoreX is offline Offline
Newbie Poster

Cannot utilize an array properly

 
0
  #1
Oct 31st, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Cannot utilize an array properly

 
0
  #2
Oct 31st, 2004
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:
  1. boolean occupied[] = new boolean[N];
Now for each index i, i + 1 is the seat number. When the seat is occupied, set occupied[i] 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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Cannot utilize an array properly

 
0
  #3
Oct 31st, 2004
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:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Cannot utilize an array properly

 
0
  #4
Oct 31st, 2004
>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?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4
Reputation: SureStoreX is an unknown quantity at this point 
Solved Threads: 0
SureStoreX SureStoreX is offline Offline
Newbie Poster

Re: Cannot utilize an array properly

 
0
  #5
Oct 31st, 2004
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]);


}




}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC