Home > Net >  Message keeps repeating through every element of array Java
Message keeps repeating through every element of array Java

Time:12-15

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:

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;
    }

CodePudding user response:

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.

CodePudding user response:

Not sure what you are trying to do here

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");

You are overwriting the ith element in the passengers array 4 times each loop.

This assignment is giving a DateTimeParseException and hence the same is being printed for each iteration:

passengers[i] = new Passenger("nomee2", "0", "19-101-02");

CodePudding user response:

In this block:

        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");
        }

You are running all the lines for every loop step. I mean, if the array has 10 elements, and inside the loop you all paserngers[i] 5 times. You are running 10*5 = 50 array assignations.

Each and every passenger[i] (but the first one) overrides the previous one.

For instace, in the first iteration, i = 0. So you run:

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

In the second iteration:

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

And like that, 10 times. In very one of the iterations you call the constructor with the wrong date (apart from the other 4 with the correct day).

  • Related