整个项目正在为机场创建系统管理员。它跟踪每个类别的机场,航班,座位区,座位和其他相关信息。初始系统是通过从以某种方式格式化的文件导入来设置的。我在正确解析文件以设置初始系统时遇到问题。数据从文件中解析出来并用作方法参数来创建对象:Airport,Airline,Flight,FlightSection和Seat。
格式是: [机场代码列表] {航空公司列表}
list-of-airport-codes :: =以逗号分隔的字符串
航空公司列表:: = airline-name1 [flightinfo-list1],airline-name2 [flightinfo-list2],airlinename3 [flightinfo-list3],...
flightinfo-list :: = flightID1 | flightdate1 | originAirportCode1 | destinationAirportCode1 [flightsectionlist1],flightID2 | flightdate2 | originAirportCode2 | destinationAirportCode2 [flightsection-list2],...
flightdate :: =年,月,日,小时,分钟
flightsection-list :: = sectionclass:seat-price:layout:rows-of-rows,...
sectionclass :: = F,B,E(对于头等舱,商务舱,经济舱)
layout :: = S,M,W(不同的已知座位布局)
示例:
[DEN,NYC,SEA,LAX] {AMER [AA1 | 2018,10,8,16,30 | DEN | LAX [E:200:S:4,F:500:S:2], AA2 | 2018,8,9,7,30 | LAX | DEN [E:200:S:5,F:500:S:3],......],UNTD [UA21 | 2018,11,8,12,30 | NYC | SEA [E:300:S:6,F:800:S:3],UA12 | 2018,8,9,7,30 | SEA | DEN [B:700:S:5,F:1200:S :2],......],FRONT [...],USAIR [...]}
我尝试使用分隔符和while循环的组合强制它。代码成功地创建了机场,第一个航空公司和Flighsections,但是当它创建第二个航空公司它崩溃,因为我没有正确循环,并且很难做到正确。我现在的代码是一团糟,如果你愿意看它,我会很感激任何建设性的意见。我的问题是什么是更好的方法来解决这个问题?一种不同的设计方法?也许更智能的方式来使用分隔符? 在此先感谢您的帮助!!
这是我尝试过的。
private void readFile(File file){
System.out.println("reading file");
Scanner tempScan;
String result;
String temp = "";
scan.useDelimiter("\\[|\\{");
try{
// AIRPORTS
result = scan.next();
tempScan = new Scanner(result);
tempScan.useDelimiter(",|\\]");
while(tempScan.hasNext()){
temp = tempScan.next();
sysMan.createAirport(temp);
}
tempScan.close();
/* AIRLINE
* FLIGHT
* FLIGHTSECTION
*/
do{
// AIRLINE (loop<flight & fsection>)
result = scan.next();
sysMan.createAirline(result);
// FLIGHT
result = scan.next();
tempScan = new Scanner(result);
do{
tempScan.useDelimiter(",|\\|");
ArrayList flightInfo = new ArrayList();
while(tempScan.hasNext()){
if(tempScan.hasNextInt()){
flightInfo.add(tempScan.nextInt());
} else {
flightInfo.add(tempScan.next());
}
}
tempScan.close();
sysMan.createFlight(sysMan.getLastAddedAirline(),(String)flightInfo.get(0), (int)flightInfo.get(1), (int)flightInfo.get(2), (int)flightInfo.get(3), (int)flightInfo.get(4), (int)flightInfo.get(5), (String)flightInfo.get(6), (String)flightInfo.get(7));
// FLIGHTSECTION (loop<itself>)
result = scan.next();
tempScan = new Scanner(result);
tempScan.useDelimiter(",|:|\\]");
ArrayList sectInfo = new ArrayList();
int i = 1;
while(!temp.contains("|")){
if(tempScan.hasNextInt()){
sectInfo.add(tempScan.nextInt());
} else {
temp = tempScan.next();
if(temp.equals(""))
break;
char c = temp.charAt(0);
sectInfo.add(c);
}
if(i == 4){
sysMan.createSection(sysMan.getLastAddedAirline(), sysMan.getLastAddedFlightID(), (char)sectInfo.get(0), (int)sectInfo.get(1), (char)sectInfo.get(2), (int)sectInfo.get(3));
i = 1;
sectInfo = null;
sectInfo = new ArrayList();
continue;
}
i++;
}
}while(!temp.equals("\\s+"));
}while(!temp.contains("\\s+"));
}catch(NullPointerException e){
System.err.println(e);
}
}
答案 0 :(得分:0)
我宁愿用regexp mathing把它打成外边界,看看,我把它打了几个级别。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
公共课Tokeni {
static String yolo = "[DEN,NYC,SEA,LAX]{AMER["
+ "AA1|2018,10,8,16,30|DEN|LAX[E:200:S:4,F:500:S:2],"
+ "AA2|2018,8,9,7,30|LAX|DEN[E:200:S:5,F:500:S:3]],"
+ "UNTD[UA21|2018,11,8,12,30|NYC|SEA[E:300:S:6,F:800:S:3],"
+ "UA12|2018,8,9,7,30|SEA|DEN[B:700:S:5, F:1200:S:2]]}";
public static void main(String[] args) {
Matcher airportCodesMatcher = Pattern.compile("\\[(.*?)\\]").matcher(yolo);
airportCodesMatcher.find();
String[] airportCodes = airportCodesMatcher.group(1).split(",");
Matcher airLinesMatcher = Pattern.compile("\\{(.*?)\\}").matcher(yolo);
airLinesMatcher.find();
String airLinesStr = airLinesMatcher.group(1);
System.out.println(airLinesStr);
Pattern airLinePattern = Pattern.compile("\\D+\\[(.*?)\\]\\]");
Matcher airLineMatcher = airLinePattern.matcher(airLinesStr);
while( airLineMatcher.find() ) {
String airLineStr = airLineMatcher.group(0).trim();
if(airLineStr.startsWith(",")) {
airLineStr = airLineStr.substring(1, airLineStr.length()).trim();
}
System.out.println(airLineStr);
Matcher airLineNameMatcher = Pattern.compile("[A-Z]+").matcher(airLineStr);
airLineNameMatcher.find();
String airLineName = airLineNameMatcher.group(0).trim();
System.out.println(airLineName);
airLineStr = airLineStr.substring(airLineStr.indexOf("[")+1, airLineStr.length());
Matcher airLineInfoMatcher = Pattern.compile("\\D+(.*?)\\]").matcher(airLineStr);
while(airLineInfoMatcher.find()) {
String airLineInfoStr = airLineInfoMatcher.group(0).trim();
if(airLineInfoStr.startsWith(",")) {
airLineInfoStr = airLineInfoStr.substring(1, airLineInfoStr.length()).trim();
}
System.out.println(airLineInfoStr);
}
}
}
}