java代码识别管道文件,扫描仪和管道分隔文件Java

I am using a pipe delimited .txt file with scheduled flights. I need to break up each line into its 3 separate parts, scheduledTime, eventType and flightID an example flight within the .txt file would look like:

12:00|ARRIVAL|A001

I have it working for one single flight but when I add more I get this error

Full Trace:

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)

now I understand what is happening here, when I have more than one flight it is skipping over the scheduledTime piece in the second line and trying to parse the eventType instead. I cannot figure out how to fix this. Below are my methods:

Method to create the event file:

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

}

}

and the method to process the event file:

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

}

}

So my question is how do I make the scanner move to the next line after reading the flightId and not think that the flightId of line 1 and scheduledTime of line two are on the same line?

Sample flight data:

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

解决方案

The Scanner object is only parsing the file based on yourDELIM variable |. It isn't splitting the file by the new line character as well.

You could use a BufferedReader object to read the file line by line and then split the text later on. Something like this:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值