If/Array Error

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

Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

If/Array Error

 
0
  #1
Aug 20th, 2009
Ok in my program i'm trying to prevent a user from adding too many objects. i have three arrays called subs airs and dests, the user shouldn't be able to have more than a total of 10 objects between them.

in order to add an object to an array the user must press a JButton on my GUI

here is the action listener for one of these buttons
  1. if(e.getSource() == addAir && (subs.length + airs.length + dests.length)<=10)
  2. {
  3. String name=JOptionPane.showInputDialog(null, "What is the name of this Aircreft Carrier?");
  4. String row=JOptionPane.showInputDialog(null, "What is the horizontal posistion of this Aircreft Carrier?");
  5. String col=JOptionPane.showInputDialog(null, "Where is the vertical posistion of this Aircreft Carrier?");
  6. String numAircraft=JOptionPane.showInputDialog(null, "How many Aircrafts are on this Aircraft Carrier?");
  7. }

to prevent the user from adding more than 10 objects ive included in the if statement a code which adds the lengths of the arrays together and then sees if the total is less than or equal to 10.

this doesn't work because the length of the arrays always exceeds 10, so how do i change this code so that i can add together the total number of objects are in the array?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,028
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: If/Array Error

 
0
  #2
Aug 20th, 2009
You will have to maintain 3 variables in which you keep the number of things actually stored in each array. This is one of the many reasons why people tend to use ArrayLists rather than naked arays.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Re: If/Array Error

 
1
  #3
Aug 20th, 2009
A second option would be to just have a item count variable which increments every time an object is added to an array. Then if the count is > 10 give appropriate responce.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

Re: If/Array Error

 
0
  #4
Aug 20th, 2009
thanks for that heres an up dated form of the action listener cos im stilling having a little problem

  1. if(e.getSource() == addSub && count <=10)
  2. {
  3. String name=JOptionPane.showInputDialog(null, "What is the name of this Submarine?");
  4. String row=JOptionPane.showInputDialog(null, "What is the horizontal posistion of this Submarine?");
  5. Scanner rowScan = new Scanner(row);
  6. int row1 = rowScan.nextInt();
  7. String col = JOptionPane.showInputDialog(null, "Where is the vertical posistion of this Submarine?");
  8. Scanner colScan = new Scanner(col);
  9. int col1 = colScan.nextInt();
  10. String depth=JOptionPane.showInputDialog(null, "What is the depth of this Submarine?");
  11. posistion=(rowScan*10)+colScan;
  12. blocks[posistion].setIcon(subPic);
  13. count++;
  14. }
my problem now is on the line which starts with position i'm trying to work out a way to take the input from the user in the row and col JOptionPanes and then to take it for the position formula so that the image icon show up in the right block

i cant use the row or col variable because they are strings and ints are needed for the formula, it wont let me use the scanner objects and i cant work out how to turn the strings for scanners into an int
Last edited by Laidler; Aug 20th, 2009 at 12:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,028
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: If/Array Error

 
0
  #5
Aug 20th, 2009
String s = "123";
int i = Integer.parseInt(s);
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 49
Reputation: Laidler is an unknown quantity at this point 
Solved Threads: 1
Laidler Laidler is offline Offline
Light Poster

Re: If/Array Error

 
0
  #6
Aug 20th, 2009
thanks for that
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 233 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC