0

I have this project that i'm making that you import your birthday, name, birthyear, and birthmonth. I'm trying to make a method that finds the most popular birthday for example, "the most popular date is the 16th with blank birthdays. I know I would have to loop around birthDayStats but how would I write that? here's my code, and the method i'm having trouble with is on the bottom.

import java.util.ArrayList;
import java.util.Collections;

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 void mostPopularDay() { //finds the most popular Day of the year
        Object obj = Collections.mostPopularDay(birthDayStats);
        System.out.println(obj);
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Stevo4586
  • 7
  • 1
  • 5
  • 2
    What is `Object obj = Collections.mostPopularDay(birthDayStats);` supposed to do? That looks like all kinds of invalid. What have you tried already to solve this? – Carcigenicate Sep 25 '17 at 13:31
  • 1
    `Collections.mostPopularDay` isn't a thing... What did you expect that to do? – OneCricketeer Sep 25 '17 at 13:32
  • [Iterating through array - java](https://stackoverflow.com/questions/14489590/iterating-through-array-java) – OH GOD SPIDERS Sep 25 '17 at 13:33
  • `if(person.getBirthDay()!=-1|| person.getBirthMonth() != -1)` should be `if(person.getBirthDay()>0 && person.getBirthMonth() > 0)` – Yahya Sep 25 '17 at 13:35
  • Possible duplicate of [How can I locate and print the index of a max value in an array?](https://stackoverflow.com/questions/8991103/how-can-i-locate-and-print-the-index-of-a-max-value-in-an-array) – PM 77-1 Sep 25 '17 at 13:41

1 Answers1

1

You have to iterate over your array which holds the days, find the highest count and return the index of that value.

public int mostPopularDay() { //finds the most popular Day of the year
    int popularDay = 0;
    int max = -1;
    for(int i = 0; i < birthDayStats.length; i++) {
        if(birthDayStats[i] > max) {
            max = birthDayStats[i];
            popularDay = i;            
        }
    }
    return popularDay + 1;  //Adding +1 since there is no 0th day of the month
}

Keep in mind, that this does only return the most popular day for birthdays, not the most popular date. But I assume that this is what you wanted.

Tobias Geiselmann
  • 2,139
  • 2
  • 23
  • 36
  • i get the error incompatible types: unexpected return value under – Stevo4586 Sep 25 '17 at 13:39
  • return popularDay; – Stevo4586 Sep 25 '17 at 13:39
  • I updated it, should work now. The method signature stated `void` where it should have been `int` for the return type. – Tobias Geiselmann Sep 25 '17 at 13:40
  • Ok cool and yes im just trying to find the most popular birth day for birthdays and then ill find the make a method for most popular birth months for birthdays – Stevo4586 Sep 25 '17 at 13:42
  • 1
    That's perfectly alright. But keep in mind, that those two results (day and month) don't necessarily correspond. Let's say you get `day = 16` and `month = 5`. That doesn't mean, that the 16th of May is the most popular birth date ;) – Tobias Geiselmann Sep 25 '17 at 13:44