0

The problem I have encountered is as follows: I have created two arrays representing docking spaces for ships. The first array (dock1[]) the ship object (shipName and size - usually Super-Container) can be saved in the array. If I want to remove object from dock1[] I enter the shipName to remove it.

But I can only remove the ship object from the first space (index 0) in the array and not from any other space i.e. index 1,2,3.

Can you help? Here's my dock class, problem in undock() if statement:

import java.util.*;

public class Main {

static Scanner scan = new Scanner(System.in);

private static Ship[] dock1 = new Ship[10];
private static Ship[] waitingList = new Ship[10];

public static void main(String[] args) {
    menu();
}

public static void menu() {


    Scanner scan = new Scanner(System.in);


    while (true) {

        System.out.println("Choose an option: 1-3");
        System.out.println("1. Dock");
        System.out.println("2. Undock");
        System.out.println("3. Status");

        int menu = scan.nextInt();
        switch (menu) {
            case 1:
                System.out.println("1. Dock");
                dock();
                break;
            case 2:
                System.out.println("2. Undock");
                undock();
                break;
            case 3:
                System.out.println("3. Status");
                printDock();
                printWaitingList();
                break;
            case 4:
                System.out.println("4. Exit");
                System.exit(0);
            default:
                System.out.println("No such option");
                break;
        }
    }
}


public static void dock() {

    System.out.println("Enter ship's name: ");
    String name = scan.nextLine();

    System.out.println("Enter ship's size: ");
    String size = scan.nextLine();

    System.out.println("Enter the ships dock:");
    //Check if the dock number is valid
    int i = Integer.valueOf(scan.nextLine());
    if (i >= 0 && i < 10 && dock1[i] == null) {
        int c = 0;
        int co = 0;
        int sco = 0;
        for (int j = 0; j < dock1.length; j++) {
            if (dock1[j] != null && dock1[j].getShipSize().equals("Cargo")) {
                c++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Container")) {
                co++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Super-Container")) {
                sco++;
            }
        }

        if (c < 10 && co < 5 && sco < 2) {
            //Add ship to the dock
            dock1[i] = new Ship(name, size);
            System.out.println("Enough space you can dock");
            System.out.println("Ship has been docked");
        } else {
            System.out.println("You cannot dock");
            waitingList(name, size);
        }

    } else {
        System.out.println("Couldn't dock");
        waitingList(name, size);
    }

}


public static void undock() {
    System.out.println("Status of ships: ");
    printDock();
    System.out.println("Enter ship's name to undock: ");
    String name = scan.nextLine();

    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
            dock1[i] = null;
            System.out.println("Ship removed");
            /// HERE CHECK IF SHIP IN DOCK
            for (int j = 0; j < waitingList.length; j++) {
                if (dock1[i] == null && waitingList[j] != null) {
                    // Add ship to the dock
                    dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                    System.out.println("Move ship from waiting list to dock 1");
                    waitingList[j] = null;
                    return;
                } else {
                 //   System.out.println("No space in dock");
                    return;
                }
            }
        } else {
            System.out.println("Ship not docked here");
            break;
        }

    }

}

public static void waitingList(String name, String size) {

    System.out.println("Dock 1 is full, ship will try to be added to Waiting List");
    for (int i = 0; i < waitingList.length; i++) {
        if (waitingList[i] == null) {
            //Add ship to the dock
            waitingList[i] = new Ship(name, size);
            System.out.println("Enough space added to waiting list");
            return;
        } else {

        }
    }
    System.out.println("No space on waiting list, ship turned away.");
}

public static void printDock() {

    System.out.println("Docks:");

    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + dock1[i].getShipName() + " " + dock1[i].getShipSize());
        }
    }
}

private static void printWaitingList() {

    System.out.println("Waiting List:");

    for (int i = 0; i < waitingList.length; i++) {
        if (waitingList[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + waitingList[i].getShipName() + " " + waitingList[i].getShipSize());
        }
    }
}
}
3
  • 1
    why not use arraylist and use remove or splice method on it. it will make your code much shorter Commented Apr 4, 2018 at 16:08
  • Thanks for your comment. But as a dock is fixed size I must use a array. Commented Apr 4, 2018 at 16:09
  • 1
    you can do that with arraylist too. Commented Apr 4, 2018 at 16:11

2 Answers 2

2

The problem is that whenever a Ship is not docked at the first position (index 0) you will not check the other positions, thats because you have a break statement if it does not equal the name of the ship to be undocked. The break statement terminates the loop and does not continue to check the other positions.

Just remove the break statement in the undock method.

EDIT

Your code should be like this.

 System.out.println("Status of ships: ");
printDock();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
boolean deleted = false;
for (int i = 0; i < dock1.length; i++) {
    if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
        dock1[i] = null;
        System.out.println("Ship removed");
        deleted = true;
        /// HERE CHECK IF SHIP IN DOCK
        for (int j = 0; j < waitingList.length; j++) {
            if (dock1[i] == null && waitingList[j] != null) {
                // Add ship to the dock
                dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                System.out.println("Move ship from waiting list to dock 1");
                waitingList[j] = null;
                return;
            } else {
                //   System.out.println("No space in dock");
                return;
            }
        }
    }

}
if (!deleted) System.out.println("No ship was removed")
Sign up to request clarification or add additional context in comments.

6 Comments

When I have done this method in the past it removes the ship but I still get the else statement - System.out.println("Ship not docked here"); first. I need a way so it removes without getting this statement
So this code should not be in that else statement, because it would print it everytime it checks that the Ship to be deleted is not at a given spot. This message should only be printed at the end, when you check that no ship was deleted.
I don't think the deleted flag is needed. Note that the OP uses a hard return in any case when the first if is true
Hey EduG, you have basically the same answer than I. That must be rewarded +1 :)
@luksch That would be true but note that if there is no ship in the waiting dock then it would not return inside the loop, it would continue and then the message would be shown at the end, but yes this code could be changed alot and have far less lines.
|
1

I see 2 mistakes:

1) You break the loop in the else statement in the undock method. 2) If you find the ship name in the first dock, then you do always return in the first iteration of the waitingList loop.

for (int i = 0; i < dock1.length; i++) {
    if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
        dock1[i] = null;
        System.out.println("Ship removed");
        /// HERE CHECK IF SHIP IN DOCK
        for (int j = 0; j < waitingList.length; j++) {
            if (dock1[i] == null && waitingList[j] != null) {
                // Add ship to the dock
                dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                System.out.println("Move ship from waiting list to dock 1");
                waitingList[j] = null;
                return;
            } else {
             //   System.out.println("No space in dock");
                return;
            }
        }
        // NOTE -> THIS ALWAYS ENDS IN A RETURN
    } else {
        System.out.println("Ship not docked here");
        break;
    }
}

I think you should leave out the break statement in order to try other docks. Also, so not return to the caller method when testing the waiting list.

So try this:

for (int i = 0; i < dock1.length; i++) {
    if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
                      dock1[i] = null;
        System.out.println("Ship removed");
        /// HERE CHECK IF SHIP IN DOCK
        for (int j = 0; j < waitingList.length; j++) {
            if (dock1[i] == null && waitingList[j] != null) {
                // Add ship to the dock
                dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                System.out.println("Move ship from waiting list to dock 1");
                waitingList[j] = null;
                return;
            } else {
             //   System.out.println("No space in dock, go on in waiting list");
              // NO RETURN HERE
            }
        }
    } else {
        System.out.println("Ship not docked here, try next dock if there is one left");
        // NO BREAK HERE
    }
}
System.out.println("Ship not docked in any dock");

5 Comments

When I have done this method in the past it removes the ship but I still get the else statement - System.out.println("Ship not docked here"); first. I need a way so it removes without getting this statement
Sorry, now I lost you. I do not understand your problem. I think my statement is correct. Your loop will never increment, since either the if statement is true and you end up in a return or you end up in the else statement and this will break the loop.
Without the 'break;' - If I enter the 'shipName' that I want to remove, it does remove the ship from the array but I first get the message from the else statement 'Ship not docked here' then i get 'ship removed' and the ship has been removed. I need it so I don't get the else statement 'Ship not docked here'
see my changed println, indicating that now the ship will be searched in the next dock. If you only want to print that it is not found at all, then you need to put the println out of the loop to the end of your method.
I updated my answer, since I think you have a second problem in your code. please check

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.