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?

Recommended Answers

All 5 Replies

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.

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.

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

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

thanks for that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.