943,753 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 427
  • Java RSS
Aug 20th, 2009
0

If/Array Error

Expand Post »
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
java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Laidler is offline Offline
51 posts
since Nov 2008
Aug 20th, 2009
0

Re: If/Array Error

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.
Featured Poster
Reputation Points: 1910
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Aug 20th, 2009
1

Re: If/Array Error

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.
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009
Aug 20th, 2009
0

Re: If/Array Error

thanks for that heres an up dated form of the action listener cos im stilling having a little problem

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Laidler is offline Offline
51 posts
since Nov 2008
Aug 20th, 2009
0

Re: If/Array Error

String s = "123";
int i = Integer.parseInt(s);
Featured Poster
Reputation Points: 1910
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Aug 20th, 2009
0

Re: If/Array Error

thanks for that
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Laidler is offline Offline
51 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Garbage collection and ResultSet, PreparedStatement, Connection
Next Thread in Java Forum Timeline: illegal start of expression





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC