-1

我有两个文件……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)
4

2 回答 2

1

类似这样的东西可以解决问题

while (scanner1.hasNextLine() && (scanner2.hasNextLine()){

    Strint line1 = scanner1.nextLine();
    String line2 = scanner2.nextLine();

    // parse line1
    String[] line1Tokens = lineS1,split(",");

    // parse line2
    String[] line2Tokens = line2.split(",");

    // Print
    String ticket = line1Tokens[0];
    String arrived1 = line1Tokens[2];
    String arraved2 = line1Tokens[3];

    String pickup1 = line2Tokens[1];
    String pickup2 = line2Tokens[2];

    System.out.println("Ticket: " + ticket + ", " + 
                       "Arrived: " + arrived1 + ":" + arived2 + " --- " + 
                       "Pickup: " + pickup1 + ":" + pickup2 + " --- " + 
                       "Cost: ");
}

printWriter.close();

这是假设所有文件行都相应匹配

编辑:使用方法

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()){

        Strint line1 = scanner1.nextLine();
        String line2 = scanner2.nextLine();

        // parse line1
        String[] line1Tokens = lineS1,split(",");

        // parse line2
        String[] line2Tokens = line2.split(",");

        // Print
        String ticket = line1Tokens[0];
        String arrived1 = line1Tokens[2];
        String arraved2 = line1Tokens[3];

        String pickup1 = line2Tokens[1];
        String pickup2 = line2Tokens[2];

        System.out.println("Ticket: " + ticket + ", " + 
                           "Arrived: " + arrived1 + ":" + arived2 + " --- " + 
                           "Pickup: " + pickup1 + ":" + pickup2 + " --- " + 
                           "Cost: ");

    }
}

public static void main(String[] args){

    File file2 = new File("someText1.txt");
    File file1 = new File("someText1.txt");

    merge(file1, file2);

}

编辑:如果您不希望merge()打印该方法,请返回一个字符串

您可以让您的方法返回 aString而不是打印

 public String merge(File file1, File file2) throws IOException {

      String entirePrint = "";

      while (scanner1.hasNextLine() && (scanner2.hasNextLine()){

          // other code      

          String oneLine = "Ticket: " + ticket + ", " + 
                           "Arrived: " + arrived1 + ":" + arived2 + " --- " + 
                           "Pickup: " + pickup1 + ":" + pickup2 + " --- " + 
                           "Cost: " + "/n";

          entirePrint += oneLine;

      }
      return entirePrint;
 }

main

 public static void main(String[] args){

      File file1 = 
      File file2 =

      YourClass yourClass = new YourClass();

      String print = yourClass.merge(file1, file2);

      System.out.println(print);
 }
于 2013-11-04T22:33:45.890 回答
0

我会将 Java 代码写入两个文件的单遍读取。我假设您知道如何编写 Java 程序并且您知道合并排序是什么,我建议您尝试将它们放在一起。反思一下,您不需要合并排序,您只需按照数据出现的顺序读取数据,然后将信息构建/添加到类似的数据结构中。

final Map<Integer, MyRecord> map = new HashMap<>();

public MyRecord acquireRecord(int ticket) {
  MyRecord rec = map.get(ticket);
  if (rec == null)
      map.put(ticket, rec = new MyRecord(ticket));
  return rec;
}

其中 MyRecord 记录有关票号的所有信息。

当您遇到麻烦时,请先使用调试器,如果这无助于发布您拥有的代码和有关它的特定问题。

于 2013-11-04T22:28:24.180 回答