Java 写入文件方法不从 arraylist 中写入任何内容
Java writing to file method writes nothing from arraylist
问题来了:文件被创建,第一行文本被写入,但是 for>if>while 逻辑循环中的文本没有写入:PassengerSeats.println(nRowNum +cSeatLetter +" " +sPassengerName);
这里是有问题的主要方法代码:
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);
这是它引用的 PrintManifest 代码:
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();
以及它从 TicketsList 中提取的数据样本,它已经被解析并存储在数组列表中:
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
布尔表达式
sFlightDate == TicketsListing.get(nCount).getDate()
将始终评估为 false,这意味着您的 if 语句中的任何内容都不会执行。应该使用 equals 方法来评估字符串相等性,就像这样
sFlightDate.equals(TicketsListing.get(nCount).getDate())
因为它比较的是字符串的内容。 == 运算符比较对象指针,在这种情况下它总是不同的。
问题来了:文件被创建,第一行文本被写入,但是 for>if>while 逻辑循环中的文本没有写入:PassengerSeats.println(nRowNum +cSeatLetter +" " +sPassengerName);
这里是有问题的主要方法代码:
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);
这是它引用的 PrintManifest 代码:
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();
以及它从 TicketsList 中提取的数据样本,它已经被解析并存储在数组列表中:
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
布尔表达式
sFlightDate == TicketsListing.get(nCount).getDate()
将始终评估为 false,这意味着您的 if 语句中的任何内容都不会执行。应该使用 equals 方法来评估字符串相等性,就像这样
sFlightDate.equals(TicketsListing.get(nCount).getDate())
因为它比较的是字符串的内容。 == 运算符比较对象指针,在这种情况下它总是不同的。