2017-07-10 2 views
0

私はパイプで区切られた.txtファイルを定期便に使用しています。私はそれが1のために働いてきたA001スキャナとパイプで区切られたファイルJava

| ARRIVAL | 00:

12:私は.txtファイル内の例のフライトは次のようになり、その3つの別々の部品、scheduledTimeeventTypeflightIDに各ラインを分割する必要があります単一の飛行が、私はもっと追加するとき、私はこのエラーに

完全なトレースを取得する:私は複数のフライトを持っているとき

java.time.format.DateTimeParseException: Text 'ARRIVAL' could not be parsed at index 0 
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source) 
at java.time.format.DateTimeFormatter.parse(Unknown Source) 
at java.time.LocalTime.parse(Unknown Source) 
at java.time.LocalTime.parse(Unknown Source) 
at edu.metrostate.ics240.p4.gaw886.sim.AirportSimulator.processEventFile(AirportSimulator.java:49) 
at edu.metrostate.ics240.p4.gaw886.sim.tests.AirportSimTests.testScenarioOne(AirportSimTests.java:22) 
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

は、今私はそれをs、ここで何が起こっているかを理解します2行目のscheduledTimeピースをキープし、代わりにeventTypeを解析しようとしました。私はこれを修正する方法を理解できません。以下は私のメソッドです:

public static final double DEPARTURE_PROB = 0.20; 
public static final double ARRIVAL_PROB = 0.15; 
public static final int ARR_RESERVE_TIME = 2; 
public static final int DEP_RESERVE_TIME = 3; 
public static final int SIM_MINUTES = 60; 
public static final long SEED = 20170620001L; 
public static final Random RAND = new Random(SEED); 
public static final String DELIM = "\\|"; 

public static void genEventFile(String fileName, double arrivalProbability, double departureProbability, 
     int minutes) { 
    int arrFlightId = 1; 
    int depFlightId = 1; 
    try (PrintWriter simFile = new PrintWriter(fileName);) { 
     LocalTime time = LocalTime.parse("00:00"); 

     for (int i = 0; i < minutes; i++) { 
      if (RAND.nextDouble() <= arrivalProbability) { 
       simFile.printf("%s|%s|%s\n", time.plusMinutes(i), Event.EventType.ARRIVAL, 
         String.format("A%03d", arrFlightId++)); 
      } 
      if (RAND.nextDouble() <= departureProbability) { 
       simFile.printf("%s|%s|%s\n", time.plusMinutes(i), Event.EventType.DEPARTURE, 
         String.format("D%03d", depFlightId++)); 
      } 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

やイベントファイルを処理する方法:イベントファイルを作成する

方法

public void processEventFile(String filename) { 
    LocalTime scheduledTime = null; 
    EventType eventType; 
    String flightID; 
    try (Scanner scanner = new Scanner(new File(filename)).useDelimiter(DELIM);) { 
     while (scanner.hasNext()) { 
      scheduledTime = LocalTime.parse(scanner.next()); 
      eventType = EventType.valueOf(scanner.next()); 
      flightID = scanner.next(); 
      flight = new Flight(scheduledTime, eventType, flightID); 
      flightQueue.add(flight); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

は、だから私の質問は、私はスキャナの動きを作るのですかどのようですflightIdを読んだ後に次の行に移動し、行1のと行2のscheduledTimeが同じ行にあるとは思わないか?

サンプル飛行データ:

00:02|DEPARTURE|D001 
00:03|ARRIVAL|A001 
00:03|DEPARTURE|D002 
00:04|DEPARTURE|D003 
00:06|DEPARTURE|D004 
00:09|DEPARTURE|D005 
00:12|ARRIVAL|A002 
00:14|ARRIVAL|A003 
00:16|DEPARTURE|D006 
00:18|ARRIVAL|A004 
00:21|DEPARTURE|D007 
00:22|ARRIVAL|A005 
00:24|DEPARTURE|D008 
00:28|ARRIVAL|A006 
00:28|DEPARTURE|D009 
00:30|ARRIVAL|A007 
00:41|ARRIVAL|A008 
00:44|DEPARTURE|D010 
00:47|DEPARTURE|D011 
00:48|DEPARTURE|D012 
00:53|DEPARTURE|D013 
00:54|ARRIVAL|A009 
00:54|DEPARTURE|D014 
00:56|DEPARTURE|D015 
00:57|DEPARTURE|D016 
+1

BufferedReaderを使用できますか?その後、一度に1行ずつ読み込み、String.split(正規表現)を使って各行をそのコンポーネントに分割することができます –

+1

フライトIDと時刻のサンプルtxtファイルを投稿することができますか? –

答えて

3

Scannerオブジェクトは、あなたのDELIM変数|に基づいてファイルを解析しています。改行文字でファイルを分割していません。

BufferedReaderオブジェクトを使用して、ファイルを1行ずつ読み込み、後でそのテキストを分割することができます。このようなもの:

public void processEventFile(String fileName) { 
    String line; 
    String[] split; 
    BufferedReader reader = null; 
    try { 
      reader = new BufferedReader(new FileReader(fileName)); 
      while((line = reader.readLine()) != null) { 
       split = line.split("\\|"); 
       flightQueue.add(new Flight(split[0], split[1], split[2])); 
      } 
    } catch(IOException e) { 
      e.printStackTrace(); 
    } finally { 
      if(reader != null) { 
       try { 
        reader.close(); 
       } catch(IOException e) { 
        e.printStackTrace(); 
       } 
      } 
    } 
} 
+0

恐ろしく、修正されました。 –

0

OR条件を使用するように区切り文字を変更できます。

public static final String DELIM = "[\\||\n]+"; 
0

java.util.Scannerを使用すると、ファイルを1行ずつ読み込むことができます。例では、ブロックtry-with-resourcesを使用して、Scannerが閉じていることを確認します。メソッドforEachRemainingは入力ファイルの次の行を返します。

public static void main(String[] args) throws ParseException { 
     try (Scanner sc = new Scanner(NewMain.class.getResourceAsStream("airport.txt"))) { 
      sc.forEachRemaining(s -> { 
       System.out.println(s.length() + " " + s); 
      }); 
     } 
    } 
関連する問題