-3

I am trying to get my code to print only names of people who were born on or after 01-01-1990. I don't know how to write the if statement condition correctly in Java. Here is my code:

Person a = new Person("John", LocalDate.parse("1969-03-15"), "+447984356766", "john@gmail.com");
Person b = new Person("Jane", LocalDate.parse("1998-04-09"), "+447220512328", "jane@gmail.com");
Person c = new Person("Harry", LocalDate.parse("1980-09-25"), "+447220012555", "harry@gmail.com");
Person d = new Person("Anne", LocalDate.parse("1978-01-12"), "+447220012222", "anne@gmail.com");
Person e = new Person("Jack", LocalDate.parse("1996-08-20"), "+447220012098", "jack@gmail.com");

Person[] personArray = new Person[5];
personArray[0] = a;
personArray[1] = b;
personArray[2] = c;
personArray[3] = d;
personArray[4] = e;

LocalDate firstDate = LocalDate.parse("1980-01-01");

for (int i = 0; i < personArray.length; i++) {
  if (getDateOfBirth().isAfter(firstDate)) {
    System.out.println(personArray[i]);
  }
}

I used multiple if statements to print the names but the array has been created to use the for loop. I just don't know how to get the code right.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Magz_k
  • 9
  • 1
    Please tell the details: how is your current code misbehaving? Does it compile? Run? Show an error? Give the wrong results? Also, best to improve your code formatting, if only so that it helps you find bugs in code structure. – Hovercraft Full Of Eels Aug 27 '23 at 17:56
  • 1
    `getDateOfBirth(...)` what method is this? Shouldn't you be calling this on your Person objects held by the array? For example, something like: `if (personArray[i].getDateOfBirth().isAfter(firstDate))` – Hovercraft Full Of Eels Aug 27 '23 at 17:57
  • This error suggests that you're just now learning the most basic concepts of Java programming. Since this site is not geared to work in place of an introduction to programming website, and since it is best to get this information from tutorials, you will probably want to start there. You can find links to great Java tutorials and other language resources in the [java info](https://stackoverflow.com/tags/java/info) section of the [tag:java] tag. Start here: [The Java™ Tutorials](https://docs.oracle.com/javase/tutorial/tutorialLearningPaths.html) – Hovercraft Full Of Eels Aug 27 '23 at 17:58
  • So, I'm guessing that your Person class has this method, `getDateOfBirth()` which returns a LocalDate object. So this means that for your code to compile and work, you are only allowed to call this method on a Person instance. Since your array holds these instances, then you must call the method on the array items in the for loop that you can get by using square bracket notation: `personArray[i]`. You're currently not doing this, and instead you're just calling the method on nothing (well, on `this`) and this is confusing your Java compiler resulting in an error. – Hovercraft Full Of Eels Aug 27 '23 at 18:04
  • Also, if your goal is "code to print only names of people who were born on or after 01-01-1990", then this, `LocalDate.parse("1980-01-01");`, should be, `LocalDate.parse("1990-01-01");`, no? – Hovercraft Full Of Eels Aug 27 '23 at 18:13

1 Answers1

0

I am trying to get my code to print only names of people who were born on or after 01-01-1990.

Use ! LocalDate#isBefore instead of LocalDate#isAfter. The !LocalDate#isBefore for the given date means on or after the given date. Using LocalDate#isAfter will cause persons born on the given date to be missed.

Also, you have missed using personArray[i] inside the if statement. You should change your code as follows:

if (!personArray[i].getDateOfBirth().isBefore(firstDate)) {
    System.out.println(personArray[i]);
}

It will print details (as per your implementation of Person#toString) of all persons born on or after firstDate. If you want to print just the names, you should use System.out.println(personArray[i].getName()) instead of System.out.println(personArray[i]).

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110