954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

If/Array Error

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

if(e.getSource() == addAir && (subs.length + airs.length + dests.length)<=10)
        {
            String name=JOptionPane.showInputDialog(null, "What is the name of this Aircreft Carrier?");
            String row=JOptionPane.showInputDialog(null, "What is the horizontal posistion of this Aircreft Carrier?");
            String col=JOptionPane.showInputDialog(null, "Where is the vertical posistion of this Aircreft Carrier?");
            String numAircraft=JOptionPane.showInputDialog(null, "How many Aircrafts are on this Aircraft Carrier?");
        }


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?

Laidler
Junior Poster in Training
51 posts since Nov 2008
Reputation Points: 10
Solved Threads: 2
 

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.

JamesCherrill
Posting Genius
Moderator
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
 

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.

PopeJareth
Light Poster
29 posts since Jun 2009
Reputation Points: 11
Solved Threads: 3
 

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

if(e.getSource() == addSub && count <=10)
        {
            String name=JOptionPane.showInputDialog(null, "What is the name of this Submarine?");
            String row=JOptionPane.showInputDialog(null, "What is the horizontal posistion of this Submarine?");
            Scanner rowScan = new Scanner(row);
            int row1 = rowScan.nextInt();
            String col = JOptionPane.showInputDialog(null, "Where is the vertical posistion of this Submarine?");
            Scanner colScan = new Scanner(col);
            int col1 = colScan.nextInt();
            String depth=JOptionPane.showInputDialog(null, "What is the depth of this Submarine?");
            posistion=(rowScan*10)+colScan;
            blocks[posistion].setIcon(subPic);
            count++;
        }

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

Laidler
Junior Poster in Training
51 posts since Nov 2008
Reputation Points: 10
Solved Threads: 2
 

String s = "123";
int i = Integer.parseInt(s);

JamesCherrill
Posting Genius
Moderator
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
 

thanks for that

Laidler
Junior Poster in Training
51 posts since Nov 2008
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You