我正在使用带有定期航班的竖线定界.txt
文件。我需要将每一行划分为3个单独的部分,scheduledTime
,eventType
和flightID
.txt
文件中的示例航班如下:
12:00 | ARRIVAL | A001
我让它适用于单个航班但是当我添加更多时我得到此错误
完整追踪:
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)
现在我明白了这里发生了什么,当我有多个航班时,它会跳过第二行中的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
之后让扫描仪移动到下一行,而不要认为第2行的flightId
和第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
答案 0 :(得分:3)
Scanner
对象仅根据您的DELIM
变量|
解析文件。它也没有按新行字符拆分文件。
您可以使用BufferedReader
对象逐行读取文件,然后稍后拆分文本。像这样:
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();
}
}
}
}
答案 1 :(得分:0)
您可以更改分隔符以使用OR条件。
public static final String DELIM = "[\\||\n]+";
答案 2 :(得分:0)
您可以使用java.util.Scanner
逐行读取文件。在示例中,我使用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);
});
}
}