User input happy birthday program For self-study homework I made a Java program that takes user input, creates people as objects, and says "Happy Birthday" to each person.

I am just wondering what mistakes I made here or how it could have been better.

PERSON CLASS

public class PeopleObjects{
    // For practice, make the people objects rather than just a list of names.

    private String name;
    private String birthDay;

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void setBirthDay(String myBirth){
        birthDay = myBirth;
    }

    public String getBirthDay(){
        return birthDay;
    }

}


MAIN

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

public class PeopleObjectsTestDrive{
    public static void main(String [] args){

        ArrayList&lt;PeopleObjects&gt; listOfPeopleObjects = new ArrayList&lt;PeopleObjects&gt;();
        Scanner input = new Scanner(System.in);

        int userChoice = 0;

        System.out.println("Enter any number other than 1 to start program."+"\n");

            while ((userChoice = input.nextInt()) != 1){

                PeopleObjects people = new PeopleObjects();

                System.out.println("\n###What is the name of the person?###\n");
                input.nextLine();//for some reason we have to use this twice to avoid bug
                people.setName(input.nextLine());

                System.out.println("\n###What is their birthday?###\n");
                people.setBirthDay(input.nextLine());
                listOfPeopleObjects.add(people);

                System.out.println("\nDo you want to quit and see list? Enter 1 for yes and 0 for no\n");
            }

            System.out.println("\n\n\n\n");

            for(int i=0; i &lt; listOfPeopleObjects.size(); i++){

                if(listOfPeopleObjects.get(i).getName().equals("Joe")){
                    System.out.println("Happy Birthday Joe! You are special! Your birthday is on "+listOfPeopleObjects.get(i).getBirthDay()+" \n");
                }else if(i%2 == 0){
                    System.out.println("Happy Birthday to "+listOfPeopleObjects.get(i).getName()+".\n");
                }else{
                    System.out.println("Happy Birthday to "+listOfPeopleObjects.get(i).getName()+" too!!!\n");

                }//end if else 
            }//end for
    }
}
PeopleObjects is an odd name for a couple reasons:


Both words are plural, even though each instance only represents a single person.
Having "object" in the name is a little redundant. It's implied that the class is representing an  object.


I'd just call the class Person.

And you didn't give PeopleObjects a constructor. You're constructing an instance of the class ahead of time, then asking for input and calling the setters. What if you forgot to set the birthday or name? They would be left as null and potentially cause problems later. If properties are required (which both name and birthday would presumably be), have them supplied by a constructor:

public Person(String name, String birthDay) {
    this.name = name;
    this.birthDay = birthDay;
}

. . .

List&lt;Person&gt; people = new ArrayList&lt;Person&gt;();

Scanner input = new Scanner(System.in);

System.out.println("Enter any number other than 1 to start program."+"\n");

while (input.nextInt() != 1){
        // This is needed since nextInt doesn't consume the newline
        input.nextLine();

        System.out.println("\n###What is the name of the person?###\n");
        String name = input.nextLine();

        System.out.println("\n###What is their birthday?###\n");
        String birthday = input.nextLine();

        Person person = new Person(name, birthday);

        people.add(person);

        System.out.println("\nDo you want to quit and see list? Enter 1 for yes and 0 for no\n");
}


I changed a few things about that loop:


As mentioned above, the person isn't constructed until the necessary data is already obtained, and the data is passed directly to the constructor.
userChoice was never used. It was just storing the users entry, but that choice was only ever used in the condition. If you really wanted it for debugging purposes, it may be appropriate, but I didn't think that it was necessary here.
I renamed the list of people to simply people. This was replacing your PeopleObjects people = new PeopleObjects(); line. Again, calling a single person people is confusing. I also changed the type of people to List, from ArrayList. Always use the broadest type possible. In this case, you don't need any operations specific to ArrayList; you're only using List methods. To see why this is important, give this a read over. Note the example used in the question.




Your other loop at the bottom is sub-optimal as well. First, you're constantly writing people.get(i). For an ArrayList, get is very fast, but those calls are causing unneeded bloat. Create an intermediate variable person in the loop to neaten things up:

for(int i = 0; i &lt; people.size(); i++) {
    Person person = people.get(i);
    String name = person.getName();

    if(name.equals("Joe")) {
        System.out.println("Happy Birthday Joe! You are special! Your birthday is on "
                           + person.getBirthDay() + " \n");

    } else if(i % 2 == 0) {
        System.out.println("Happy Birthday to " + name + ".\n");

    } else {
        System.out.println("Happy Birthday to " + name + " too!!!\n");
    }
}


I ended up creating a name variable as well since the name of the person was needed in multiple places.

You also had inconsistent spacing that I corrected. Some places had everything all smooshed together, and then other places (like with ==) had spacing. I added spaces around all the operators, then broke up one of the longer lines. I also added empty lines after each println call so the cases were a little more distinct.

Unless you need the i % 2 == 0 check to give a special message to every arbitrary second person, this loop could be slightly simplified by using an enhanced for-loop that doesn't rely on indices:

// Read as "for each person in people"
for(Person person : people) {
    String name = person.getName();

    if(name.equals("Joe")) {
        System.out.println("Happy Birthday Joe! You are special! Your birthday is on "
                           + person.getBirthDay() + " \n");

    } else {
        System.out.println("Happy Birthday to " + name + " too!!!\n");
    }
}