我有两个文件……arrivals.txt 和picks.txt
文件1:
TickeNum,Loc,DayofMonth,Hour
1421,127,12,8
1422,108,12,8
1423,110,12,9
1424,112,12,9
1425,101,12,9
1426,105,12,9
1427,106,12,10
1428,109,12,10
1429,102,12,11
1430,107,12,12
1431,122,12,14
1432,128,12,17
1433,132,12,19
1434,136,12,21
1435,141,12,23
1436,142,13,6
和
文件 2:
TickeNum,DayofMonth,Hour
1422,12,9
1428,12,12
1423,12,13
1429,12,14
1431,12,16
1424,12,17
1421,12,18
1425,12,19
1426,13,21
1434,13,7
1435,13,9
1436,13,16
1430,13,19
1432,13,20
目标输出
Ticket: 1422, Arrived: 12: 8 --- Pickup: 12: 9 ------ Cost: 7.00
Ticket: 1428, Arrived: 12:10 --- Pickup: 12:12 ------ Cost: 10.00
Ticket: 1423, Arrived: 12: 9 --- Pickup: 12:13 ------ Cost: 15.00
Ticket: 1429, Arrived: 12:11 --- Pickup: 12:14 ------ Cost: 13.00
Ticket: 1431, Arrived: 12:14 --- Pickup: 12:16 ------ Cost: 10.00
Ticket: 1424, Arrived: 12: 9 --- Pickup: 12:17 ------ Cost: 22.00
Ticket: 1421, Arrived: 12: 8 --- Pickup: 12:18 ------ Cost: 22.00
Ticket: 1425, Arrived: 12: 9 --- Pickup: 12:19 ------ Cost: 22.00
Ticket: 1426, Arrived: 12: 9 --- Pickup: 13:21 ------ Cost: 52.00
Ticket: 1434, Arrived: 12:21 --- Pickup: 13: 7 ------ Cost: 35.00
Ticket: 1435, Arrived: 12:23 --- Pickup: 13: 9 ------ Cost: 29.00
Ticket: 1436, Arrived: 13: 6 --- Pickup: 13:16 ------ Cost: 22.00
Ticket: 1430, Arrived: 12:12 --- Pickup: 13:19 ------ Cost: 52.00
Ticket: 1432, Arrived: 12:17 --- Pickup: 13:20 ------ Cost: 48.00
任何帮助表示赞赏...
到目前为止我的代码......(无论如何你可能需要......)
主要的:
public class LotDriver
{
public static ParkingLot lot1;
public static void main(String[] args) throws IOException
{
System.out.println("Adam Klein's Parking Lot Revenue Checker - Previously Don Smith's");
File file1 = new File("arrivals.txt");
File file2 = new File("pickups.txt");
lot1 = new ParkingLot(101, 50);
lot1.readArrivals("arrivals.txt");
System.out.println("Total Cars parked: " + lot1.getTotalCars());
lot1.readPickups("pickups.txt");
lot1.merge(file1, file2);
lot1.outputCars();
System.out.println("Total Cars remaining: " + lot1.getTotalCars());
System.out.printf("\nTotal Revenue: %10.2f\n",lot1.getRevenue());
}
}`
在另一个文件中......我有这个合并方法
public static void merge(File file1, File file2) throws IOException {
Scanner scanner1 = new Scanner(file1);
Scanner scanner2 = new Scanner(file2);
while (scanner1.hasNextLine() && scanner2.hasNextLine())
{
String trash = scanner1.nextLine();
String trash2 = scanner2.nextLine();
String line1 = scanner1.nextLine();
String line2 = scanner2.nextLine();
// parse line1
String[] line1Tokens = line1.split(",");
// parse line2
String[] line2Tokens = line2.split(",");
// Print
String ticket = line1Tokens[0];
String arrived1 = line1Tokens[2];
String arrived2 = line1Tokens[3];
String pickup1 = line2Tokens[1];
String pickup2 = line2Tokens[2];
System.out.println("Ticket: " + ticket + ", " +
"Arrived: " + arrived1 + ":" + arrived2 + " --- " +
"Pickup: " + pickup1 + ":" + pickup2 + " --- " +
"Cost: ");
}
}
它抛出一个错误:
Ticket: 1421, Arrived: 12:8 --- Pickup: 12:9 --- Cost:
Ticket: 1423, Arrived: 12:9 --- Pickup: 12:13 --- Cost:
Ticket: 1425, Arrived: 12:9 --- Pickup: 12:16 --- Cost:
Ticket: 1427, Arrived: 12:10 --- Pickup: 12:18 --- Cost:
Ticket: 1429, Arrived: 12:11 --- Pickup: 13:21 --- Cost:
Ticket: 1431, Arrived: 12:14 --- Pickup: 13:9 --- Cost:
Ticket: 1433, Arrived: 12:19 --- Pickup: 13:19 --- Cost:
java.lang.ArrayIndexOutOfBoundsException: 1
at ParkingLot.merge(ParkingLot.java:65)
at LotDriver.main(LotDriver.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)