1

I'm trying to make a program in java that you can add people's birthdays, names, birthmonths, and birthyears. I'm having a hard time trying to come up with the code to remove an object from the arraylist here is the following code. How would I go about writing the removePerson method?

import java.util.ArrayList;

public class Analyzer {
    // instance variables - replace the example below with your own
    private final static int DAYS_PER_MONTH = 31;
    private final static int MONTHS_PER_YEAR = 12;
    private int[] birthDayStats;
    private int[] birthMonthStats;
    private ArrayList<Person> people;

    /**
     * Constructor for objects of class Analyzer
     */
    public Analyzer() {
        this.people = new ArrayList<Person>();
        this.birthDayStats = new int[Analyzer.DAYS_PER_MONTH];
        this.birthMonthStats = new int[Analyzer.MONTHS_PER_YEAR];
    }

    public void addPerson(String name, int birthDay, int birthMonth, int
            birthYear) {
        Person person = new Person(name, birthDay, birthMonth, birthYear);
        if (person.getBirthDay() != -1 || person.getBirthMonth() != -1) {
            people.add(person);
            birthMonthStats[birthMonth - 1]++;
            birthDayStats[birthDay - 1]++;
        } else {
            System.out.println("Your current Birthday is " + birthDay + " or "
                    + birthMonth + " which is not a correct number 1-31 or 1-12 please " +
                    "put in a correct number ");
        }
    }

    public void printPeople() { //prints all people in form: “  Name: Tom   Month: 5   Day: 2  Year: 1965”
        int index = 0;
        while (index < people.size()) {
            Person person = (Person) people.get(index);
            System.out.println(person);
            index++;
        }
    }

    public void printMonthList() { //prints the number of people born in each month
        // Sample output to the right with days being similar
        int index = 0;
        while (index < birthMonthStats.length) {
            System.out.println("Month number " + (index + 1) + " has " +
                    birthMonthStats[index] + " people");
            index++;
        }
    }

    public Person removePerson(String name) {// removes the person from the arrayList
    }
}
4
  • and the person class? does that override the methods for that?? Commented Sep 26, 2017 at 13:22
  • 1. You use an iterator to iterate through your list 2. you compare if the name you passed as an argument equals the name of the person of the current iteration 3. you use the iterator to remove the person. <- This should give you enough hints without handing you your homework soluton on a plate (although i bet someone will do that soon enough) Commented Sep 26, 2017 at 13:24
  • LOL ok will try those ! Commented Sep 26, 2017 at 13:25
  • You could use a Map instead of an ArrayList Commented Sep 26, 2017 at 13:26

2 Answers 2

1
/**
 * Removes the {@code Person} with the given {@code name} from the list
 * @param name the {@code Person}'s name
 * @return the {@code Person} removed from the list or {@code null} if not found
 */
public Person removePerson(String name) {
    if (name != null) {
        for (Iterator<Person> iter = people.iterator(); iter.hasNext(); ) {
            Person person = iter.next();
            if (name.equalsIgnoreCase(person.getName())) {
                iter.remove();
                return person;
            }
        }
    }
    return null;
}

See the java.util.Iterator#remove() method.


Tuesday learning bonus:

If you want to look for a name faster in your list, you should consider to use a java.util.Map implementation:

HashMap<String,Person> people;

You can add Person objects in a smart way in order to make your search case insensitive:

people.put(name.toLowerCase(), new Person(name, ...));

... and your removePerson method become:

public Person removePerson(String name) {
    if (name != null)
        name = name.toLowerCase();
    return people.remove(name);
}

See the java.util.Map#remove() method.

Sign up to request clarification or add additional context in comments.

5 Comments

same as @OH GOD SPIDERS's comment
someone that understands that i'm just learning java and needed help literally that's all.
exactly what im saying
is it possible to remove the stats from the array too? Because the method you gave just gets rid of the name and not the birth year, birth day, and birth month.
it should be easy to modify the code provided to get what is required, shouldn't it? The given method simply meets the question requirements. You can think of a new method which removes all the Persons found for a given stat or a method that accepts a Person as argument which looks for its non-null properties in order to identify the candidates for the deletion
1

If you are using Java 1.8. It's pretty simple way to do. This will remove Person having 'name' from your list.

 people.removeIf(x -> name.equalsIgnoreCase(x.getName()));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.