Jonathan Rauscher
Jonathan Rauscher

Reputation: 157

Java writing to file method writes nothing from arraylist

So here's the problem: File gets created, first line of text gets written, but the text within the logic loop of for>if>while doesn't write: PassengerSeats.println(nRowNum +cSeatLetter +" " +sPassengerName);

Here's the main method code in question:

else if (nMainChoice == 4) {
    System.out.print("Please enter a flight number: ");
    nMainFlight = input.nextInt();
    input.nextLine();
    System.out.println("Please enter a date of departure: ");
    System.out.print("Month: (i.e For January, enter 1): ");
    sMonth = input.nextLine();
    System.out.print("Day: (i.e for the 15h, enter 15): ");
    sDay = input.nextLine();
    System.out.print("Year: (i.e for 2015, enter 15): ");
    sYear = input.nextLine();
    sDate = sMonth +"/" +sDay +"/" +sYear;
    System.out.println(sDate);

    PrintManifest newManifest = new PrintManifest(TicketsList,
                    FlyersList, nMainFlight, sDate, sMonth, sDay, sYear);

And here's the PrintManifest code it refers to:

public PrintManifest(ArrayList<Tickets> TicketsListing, ArrayList<Flyers> FlyersListing, 
        int nFlightID, String sFlightDate, String sFMonth, String sFDay, 
        String sFYear) throws Exception{

    String sFlightManifest = "DataFiles/Manifest-nFlightID-sFMonth-sFDay-sFYear.txt";
    File FlightManifest = new File(sFlightManifest);
    PrintWriter PassengerSeats = new PrintWriter(FlightManifest);

    PassengerSeats.println("Flight manifest for #" +nFlightID +" on " +sFlightDate);

    for (nCount = 0; nCount < TicketsListing.size(); nCount++) {
        if (nFlightID == TicketsListing.get(nCount).getFlightNumber() 
            && sFlightDate == TicketsListing.get(nCount).getDate()) {

        nRowNum = TicketsListing.get(nCount).getRow();
        cSeatLetter = TicketsListing.get(nCount).getSeat();

        while (TicketsListing.get(nCount).getFlyerID() != FlyersListing.get(nCountFly).getID()) {
            nCountFly++;
        } //End while loop

        sPassengerName = FlyersListing.get(nCountFly).getFirst() +" " 
                +FlyersListing.get(nCountFly).getLast();

        PassengerSeats.println(nRowNum +cSeatLetter +" " +sPassengerName);
        } //End if 
    } //End for loop

    PassengerSeats.close();

And a sample of the data it's pulling from TicketsList, which is already parsed and stored within an arraylist:

19836;1258;1359;1/2/15;A;5;A
19837;1215;1359;1/2/15;A;6;C
19838;1245;438;1/11/15;M;15;F
19839;1129;1014;1/5/15;M;17;F
19840;1139;703;1/11/15;M;14;C
19841;1353;689;1/11/15;F;3;D
19842;1296;1014;1/2/15;F;4;F

Upvotes: 1

Views: 92

Answers (1)

Chris Z
Chris Z

Reputation: 357

The boolean expression

sFlightDate == TicketsListing.get(nCount).getDate()

will always evaluate to false, which means nothing in your if statement will execute. The equals method should be used to evaluate string equality, like this

sFlightDate.equals(TicketsListing.get(nCount).getDate())

because it compares the contents of the strings. The == operator compares the object pointer, which will always be different in this case.

Upvotes: 3

Related Questions