Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Message keeps repeating through every element of array Java

I’m new at Java and I have an array of objects of Passenger constructor class. I’m trying to display an error message if a Passenger object is being created and the birth date inserted isn’t in the correct format I specify below. The problem is the error message keeps repeating through the entire array when I only have one birth date not formatted correctly.

This is my main class:

public class IPOO_P2 {

    private static Passenger[] passengers = new Passenger[10];

    public static void main(String[] args) {

        SubwayManager man1 = new SubwayManager(passengers);

        for (int i = 0; i < passengers.length; i++) {

            passengers[i] = new Passenger("nomee", "12", "2011-10-02");
            passengers[i] = new Passenger("nomee2", "1", "1957-10-02");
            passengers[i] = new Passenger("nomee2", "0", "19-101-02");
            passengers[i] = man1.createPassenger("name", "3", "1997-10-02");
            passengers[i] = man1.createPassenger("name", "3", "1997-10-02");
        }
    }
}

The Passenger class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Passenger {

    private String name;
    private String nif;
    private String birthDate;
    private Trip[] tripsLog;
    private boolean student;

    public Passenger(String name, String nif, String birthDate) {
        if (name != null || name != "") {

           if(isDateValid(birthDate)) {
               
                this.name = name;
                this.nif = nif;
                this.birthDate = birthDate;
                this.student = false;
                this.tripsLog = new Trip[10];
                
            } 
           else{ 
               System.out.println("Birth Date formatt should be: yyyy-MM-dd");
           }
        }
    }

    public boolean isDateValid(String bDate) {

        if(bDate != null){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        try {
            LocalDate.parse(bDate, formatter);
            return true;
        } catch (DateTimeParseException e) {

            return false;
        }
    }return false;
    }

>Solution :

The problem is that your For loop is repeatedly executing the code that constructs the Passenger objects 10 times. So you will see the same error message displayed for 10 times if there is one bad date in a Passenger constructor.

Remove the For loop from the code creating the Passenger objects and create the Passenger objects individually like so:

            passengers[0] = new Passenger("nomee", "12", "2011-10-02");
            passengers[1] = new Passenger("nomee2", "1", "1957-10-02");
            passengers[2] = new Passenger("nomee2", "0", "19-101-02");
            passengers[3] = man1.createPassenger("name", "3", "1997-10-02");
            passengers[4] = man1.createPassenger("name", "3", "1997-10-02");

That way you will see the error message just one time for each bad date supplied to a Passenger constructor.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading