0

I need to remove an element from an ArrayList, based on the user input. So what I have is an ArrayList where a user can register dogs. Then if the user wants to remove a dog, he/she should be able to do it by using the command "remove dog" followed by the name of the dog.

I have tried using an iterator, but when using it only the else statement is used and "Nothing has happened" is printed out on the screen.

import java.util.Iterator;
import java.util.Scanner;
import java.util.ArrayList;


public class DogRegister {
    ArrayList<Dog> dogs = new ArrayList<>();

    private Scanner keyboard = new Scanner(System.in);

    public static void initialize() {
        System.out.println("Welcome to this dog application");
    }


    private boolean handleCommand(String command) {
        switch (command) {
            case "One":
                return true;
            case "register new dog":
                registerNewDog();
                break;
            case "increase age":
                increaseAge();
                break;
            case "list dogs":
                listDogs();
                break;
            case "remove dog":
                removeDog();
                break;
            default:
                System.out.println("Error: Unknown command");
        }
        return false;
    }


    private void registerNewDog() {
        System.out.print("What is the dog's name? ");
        String dogNameQuestion = keyboard.nextLine().toLowerCase().trim();


        System.out.print("Which breed does it belong to? ");
        String dogBreedQuestion = keyboard.nextLine().toLowerCase().trim();

        System.out.print("How old is the dog? ");
        int dogAgeQuestion = keyboard.nextInt();

        System.out.print("What is its weight? ");
        int dogWeightQuestion = keyboard.nextInt();


        keyboard.nextLine();

        Dog d = new Dog(dogNameQuestion, dogBreedQuestion, dogAgeQuestion, 
dogWeightQuestion);
        dogs.add(d);

        System.out.println(dogs.get(0).toString());
    }


private void removeDog() {
        System.out.print("Enter the name of the dog ");
        String removeDogList = keyboard.nextLine();
        for (Iterator<Dog> dogsIterator = dogs.iterator(); 
dogsIterator.hasNext();) {
            if (removeDogList.equals(dogsIterator)) {
                System.out.println("The dog has been removed ");
                break;
            } else {
                System.out.println("Nothing has happened ");
                break;
            }
        }
    }


    public void closeDown() {
        System.out.println("Goodbye!");
    }

    public void run() {
        initialize();
        runCommandLoop();
    }


    public static void main(String[] args) {
        new DogRegister().run();
    }
}
1
  • Change this line: if (removeDogList.equals(dogsIterator)) {. And the break in the else branch is probably wrong too. Commented Jan 5, 2019 at 10:09

2 Answers 2

2

You compare a String will an Iterator as said by JB Nizet :

if (removeDogList.equals(dogsIterator)) {

It will never return true. Besides even invoking next() on the iterator will not solve the problem as a String cannot be equal to a Dog object either. Instead of, compare String with String when you use equals() and invoke Iterator.remove() to effectively remove the current iterated element.

That should be fine :

private void removeDog() {
    System.out.print("Enter the name of the dog ");
    String removeDogList = keyboard.nextLine();

    for (Iterator<Dog> dogsIterator = dogs.iterator();dogsIterator.hasNext();) {   
        Dog dog = dogsIterator.next();
        if (removeDogList.equals(dog.getName())) {
            dogsIterator.remove(); 
            System.out.println("The dog has been removed");
            return;
        } 
     }

     System.out.println("Nothing has been removed");
}
Sign up to request clarification or add additional context in comments.

15 Comments

@Dorian Gray. Agreed. Not seen that
Okay but when I try to change the iterator to Iterator<String>, then I still receive an error. I have looked at the documentation, but I don't really understand how to solve this issue
@Hiwa_X Thats because you are iterating over an ArrayList<Dog>. Did you even try the code above?
@DorianGray Yes I've tried the code, my editor cannot resolve the string getName. I do have a getName method in my Dog Class, that is returning name
@davidxxx @Hiwa_X Seems it should be if (removeDogList.equals(dog.getName())), exactly calling that method you mentioned. Couldn't you fix it for yourself?
|
1

An Iterator<Dog> can't possibly be equal to a String: they don't even have the same type.

Only a String can be equal to a String.

You want to get the next value of the iterator, which is a Dog. Then you want to compare the name of the dog with the String input.

And then you want to remove the dog, using the iterator's remove() method. Read the javadoc of Iterator.

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.