0

标题中是我的数组的错误。这是什么意思????

还我如何解析文件以删除最后的所有垃圾,如 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 的所有问题?

4

3 回答 3

0

第一个问题:这意味着您尝试访问大于数组长度的数组元素。

第二。您可以使用String.contains(CharSequence s)检查您阅读的文本是否包含逗号,如果没有,请在循环中跳过它,当然如果您所有的有效行都必须用逗号分隔值。

于 2013-11-05T06:38:15.553 回答
0

如果要删除空格,只需像这样使用 trim() 。

String newVar = oldVar.trim();

如果这不会删除新行和回车,请使用:

newVar = newVar.replaceAll("\\r|\\n", "");

这将删除每一行分隔符。

如果您想在阅读文件时跳过这些行,这里有很多答案和检查方法: 如何检查 Java 字符串是否全是空格?

于 2013-11-05T06:52:24.200 回答
0

您的变量“line1”或“line2”之一不包含拆分字符“,”导致拆分的字符串 [] 大小仅为 1,并且包含整个原始字符串。在这种情况下,当您访问索引为 1 或 2 的字段时会出现异常。当您读取文件并到达最后一行是空的时,通常会发生这种情况。我建议您检查数组的大小并在不适用的情况下跳过您的代码。

于 2013-11-05T07:01:32.160 回答