| | |
Cannot utilize an array properly
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 4
Reputation:
Solved Threads: 0
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.
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:
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.
Java Syntax (Toggle Plain Text)
boolean occupied[] = new boolean[N];
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
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:
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?
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
•
•
Join Date: Oct 2004
Posts: 4
Reputation:
Solved Threads: 0
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]);
}
}
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]);
}
}
![]() |
Similar Threads
- Array Instruction Please (C++)
- C++ sequential file / array (C++)
- Populating an array with results from a mysql query... (PHP)
- Void* Array - HELP! (C)
- Javascript array from sql query (JSP)
Other Threads in the Java Forum
- Previous Thread: Please help me whit this java question.
- Next Thread: Spelling Test using a button GUI environment..how do I create a brand new window?
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






