标题中是我的数组的错误。这是什么意思????
还我如何解析文件以删除最后的所有垃圾,如 ENTER ENTER ENTER 空格......(就像回车几次,空行......我不知道那些叫什么......他们肯定不是空白..)
编辑:我现在要睡觉了。我会早起,并尝试在我得到的帮助下解决它。谢谢大家!
while (scanner1.hasNextLine() && scanner2.hasNextLine())
{
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];
这是整个方法:
public static void merge(File file1, File file2) throws IOException
{
Scanner scanner1 = new Scanner(file1);
Scanner scanner2 = new Scanner(file2);
String trash = scanner1.nextLine();
String trash2 = scanner2.nextLine();
while (scanner1.hasNextLine() && scanner2.hasNextLine())
{
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: 1422, Arrived: 12:8 --- Pickup: 12:12 --- Cost:
Ticket: 1423, Arrived: 12:9 --- Pickup: 12:13 --- Cost:
Ticket: 1424, Arrived: 12:9 --- Pickup: 12:14 --- Cost:
Ticket: 1425, Arrived: 12:9 --- Pickup: 12:16 --- Cost:
Ticket: 1426, Arrived: 12:9 --- Pickup: 12:17 --- Cost:
Ticket: 1427, Arrived: 12:10 --- Pickup: 12:18 --- Cost:
Ticket: 1428, Arrived: 12:10 --- Pickup: 12:19 --- Cost:
Ticket: 1429, Arrived: 12:11 --- Pickup: 13:21 --- Cost:
Ticket: 1430, Arrived: 12:12 --- Pickup: 13:7 --- Cost:
Ticket: 1431, Arrived: 12:14 --- Pickup: 13:9 --- Cost:
Ticket: 1432, Arrived: 12:17 --- Pickup: 13:16 --- Cost:
Ticket: 1433, Arrived: 12:19 --- Pickup: 13:19 --- Cost:
Ticket: 1434, Arrived: 12:21 --- Pickup: 13:20 --- Cost:
java.lang.ArrayIndexOutOfBoundsException: 1
at ParkingLot.merge(ParkingLot.java:45)
at LotDriver.main(LotDriver.java:17)
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)
编辑:所以我开始用我获得的所有信息来纠正它。
到目前为止,这是我的方法代码。
public static void merge(File file1, File file2) throws IOException
{
Scanner scanner1 = new Scanner(file1);
Scanner scanner2 = new Scanner(file2);
String trash = scanner1.nextLine();
String trash2 = scanner2.nextLine();
while (scanner1.hasNextLine() && scanner2.hasNextLine())
{
String line1 = scanner1.nextLine();
String line2 = scanner2.nextLine();
String ticket = "";
String arrived1 = "";
String arrived2 = "";
String pickup1 = "";
String pickup2 = "";
// parse line1
// String[] line1Tokens = line1.split(",");
// parse line2
// String[] line2Tokens = line2.split(",");
if (line1.contains(","))
{
String[] line1Tokens = line1.split(",");
// Print
ticket = line1Tokens[0];
arrived1 = line1Tokens[2];
arrived2 = line1Tokens[3];
}
if (line2.contains(","))
{
String[] line2Tokens = line2.split(",");
pickup1 = line2Tokens[1];
pickup2 = line2Tokens[2];
}
System.out.println("Ticket: " + ticket + ", " +
"Arrived: " + arrived1 + ":" + arrived2 + " --- " +
"Pickup: " + pickup1 + ":" + pickup2 + " --- " +
"Cost: ");
}
}
但是,它仍然给我一个错误......
java.lang.ArrayIndexOutOfBoundsException: 1
at ParkingLot.merge(ParkingLot.java:45)
at LotDriver.main(LotDriver.java:17)
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)
这里有什么帮助吗?如何设置数组以解决 -1 的所有问题?