0

I'm trying to add multiple values to an ArrayList via an if/else statement. The variable numberOfShips declares how many as a field. However, when I print out .size(), the size is only 1, with the latest added object being the one in there. I can't figure out what I'm doing wrong. I know ArrayList increases implicitly in size as elements are added, so that can't be it.

public ArrayList<Ship> createFleet(int choice) {
    ArrayList<Ship> fleet = new ArrayList<Ship>();
    if (count < numberOfShips && choice > 0 && choice < 5) {
        if (choice == 1) {
            Ship ac = new Ship("Aircraft carrier", 5, false);
            fleet.add(ac);
            count++;
            System.out.println("Aircraft carrier has been added to fleet.");
        } else if (choice == 2) {
            Ship bs = new Ship("Battleship", 4, false);
            fleet.add(bs);
            count++;
            System.out.println("Battleship has been added to fleet.");
        } else if (choice == 3) {
            Ship sm = new Ship("Submarine", 3, false);
            fleet.add(sm);
            count++;
            System.out.println("Submarine has been added to fleet.");
        } else if (choice == 4) {
            Ship ds = new Ship("Destroyer", 3, false);
            fleet.add(ds);
            count++;
            System.out.println("Destroyer has been added to fleet.");
        } else if (choice == 5) {
            Ship sp = new Ship("Patrol Boat", 2, false);
            fleet.add(sp);
            count++;
            System.out.println("Patrol boat has been added to fleet.");
        }
    } else {
        System.out.println("Not an option.");
    }
    return fleet;
}
2
  • Have you seen the switch statement? it might help you here. Commented Oct 13, 2014 at 13:27
  • 3
    If you are passing in the choice and expecting the method to add a ship to an existing list, you need to pass in the list instead of creating a new list in your method. Commented Oct 13, 2014 at 13:34

3 Answers 3

1

You are creating a new ArrayList every time you call the method. You need to maintain an ArrayList of ships outside your method.

public ArrayList<Ship> createFleet(int choice) {
    ArrayList<Ship> fleet = new ArrayList<Ship>(); //Here you create a new ArrayList and return it with a single Ship in it.

You need a global scoped ArrayList variable:

private List<Ship> fleet = new ArrayList<Ship>();

public ArrayList<Ship> createFleet(int choice) {
    if (count < numberOfShips && choice > 0 && choice < 5) {
        if (choice == 1) {
            Ship ac = new Ship("Aircraft carrier", 5, false);
            fleet.add(ac);
            count++;
            System.out.println("Aircraft carrier has been added to fleet.");
        } else if (choice == 2) {
            Ship bs = new Ship("Battleship", 4, false);
            fleet.add(bs);
            count++;
            System.out.println("Battleship has been added to fleet.");
        } else if (choice == 3) {
            Ship sm = new Ship("Submarine", 3, false);
            fleet.add(sm);
            count++;
            System.out.println("Submarine has been added to fleet.");
        } else if (choice == 4) {
            Ship ds = new Ship("Destroyer", 3, false);
            fleet.add(ds);
            count++;
            System.out.println("Destroyer has been added to fleet.");
        } else if (choice == 5) {
            Ship sp = new Ship("Patrol Boat", 2, false);
            fleet.add(sp);
            count++;
            System.out.println("Patrol boat has been added to fleet.");
        }
    } else {
        System.out.println("Not an option.");
    }
    return fleet;
}

My suggestion is to create a Fleet class which contains the reference for the ArrayList:

public class Fleet {
    private List<Ship> internalFleet = new ArrayList<Ship>();
    private static int MAX_SHIPS = 10;

    public void addShip(int choice){
        if (internalFleet.size() < MAX_SHIPS) {
            Ship ship;
            switch(choice){
                case 1: ship = new Ship("Aircraft carrier", 5, false);
                        break;
                case 2: ship = new Ship("Battleship", 4, false);
                        break;
                case 3: ship = new Ship("Submarine", 3, false);
                        break;
                case 4: ship = new Ship("Destroyer", 3, false);
                        break;
                case 5: ship = new Ship("Patrol Boat", 2, false);
                        break;
                default: System.out.println("Not an option.");
            }
            if(ship!=null){
                internalFleet.add(ship);
                System.out.println(ship.getName() + " has been added to fleet.");
            }
        }
    }
    public ArrayList<Ship> getFleet(){
        return internalFleet;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your createFleet method only adds a single ship to the list, based on the input choice. If you want to add multiple ships, you'll to run that code in some loop. It's not clear why you are passing the choice to the method in the first place if you want multiple ships to be created.

If your intent was to create multiple ships of the same type, you can do :

ArrayList<Ship> fleet = new ArrayList<Ship>();
for (int count=0; count < numberOfShips ; count++) {
    if (choice == 1) ...
}

Your code can be further improved by replacing the long if .. else if ... else if with a switch statement.

6 Comments

If I put the code in the loop and i input choice, it chooses the same object numberOfShips times. I need to be able to choose different Ship objects
@user2740689 Where is the loop? If you have a loop that calls createFleet multiple times, each call would create a new list with a single ship.
It's in a different class the calls the method.
The loop is: while(battleship.count < battleship.numberOfShips) { fleet = new ArrayList<Ship> (battleship.createFleet(serverEntry.nextInt())); }
@user2740689 Then that's the problem. Your method creates a new empty list each time it is called.
|
0
for(int count=0; count < numberOfShips ; count++){
    if (choice == 1) {
        fleet.add(new Ship("Aircraft carrier", 5, false));
        System.out.println("Aircraft carrier has been added to fleet.");
    }
    else if ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.