1 回答
TA贡献1815条经验 获得超6个赞
已解决:显然,问题是由调用一个方法引起的,该方法读取文件并在另一个从文件读取的方法中对树执行 add() 。我编辑了 import_events() 和 import_cmd() 方法,现在一切正常。
private HashSet<Event> import_events(String path) throws FileNotFoundException, IOException, ParseException {
FileReader fr1;
BufferedReader br1;
String current;
String[] tokens;
HashSet<Event> set = new HashSet<Event>();
fr1 = new FileReader(path);
br1 = new BufferedReader(fr1);
current = br1.readLine();
if (current == null) {
if (this.controller != null) {
controller.get_message("Warning: event file is empty, no events to import");
}
br1.close();
fr1.close();
return set;
}
InputLine il;
while (current != null) {
current = current.trim();
if (this.controller != null) {
il = new InputLine(current, controller);
} else {
il = new InputLine(current);
}
if (il.line_ok()) {
tokens = current.split(Pattern.quote(" "));
RegState reg_state = RegState.valueOf(tokens[0]);
String user_id = tokens[3];
LogState log_state = LogState.valueOf(tokens[1]);
InputLine.d_format.setLenient(false);
Date timestamp = InputLine.d_format.parse(tokens[2]);
String[] latlong = tokens[4].split(",");
double lat = Double.parseDouble(latlong[0]);
double longi = Double.parseDouble(latlong[1]);
Position pos = Position.create(lat, longi);
if (pos == null) {
if (this.controller != null) {
controller.get_message("Error: invalid coordinates at "+current+", event ignored");
}
break;
}
Emotion emotion = Emotion.valueOf(tokens[5]);
Event event = new Event(reg_state,log_state, timestamp, user_id, pos, emotion);
set.add(event);
}
current = br1.readLine();
}
br1.close();
fr1.close();
return set;
}
现在 import_events() 返回文件中包含的事件的 HashSet。
import_cmd() 使用 HashSet 作为在树上调用 addAll() 的参数。
void import_cmd() throws NumberFormatException, ParseException {
FileReader fr;
BufferedReader br;
String current;
try {
fr = new FileReader(cmd_path);
br = new BufferedReader(fr);
} catch (FileNotFoundException e) {
if (this.controller != null) {
controller.get_message("Error: file not found at this location "+cmd_path.getAbsolutePath());
}
return;
}
InputLine line;
Set<Event> toAdd=new HashSet<Event>();
try {
current = br.readLine();
while (current != null) {
if (this.controller != null) {
line = new InputLine(current, controller);
} else {
line = new InputLine(current);
}
if (line.cmd_check() == 1) {
String extracted = line.getIn_line().substring(line.getIn_line().indexOf("(")+1, line.getIn_line().indexOf(")"));
String path = this.event_path.getAbsolutePath()+File.separator+extracted;
try {
toAdd=import_events(path);
if (this.controller != null) {
controller.get_message("File "+ extracted + " successfully imported ");
}
} catch (FileNotFoundException e) {
if (this.controller != null) {
controller.get_message("Error: file not found at "+path);
}
} catch (IOException ioe) {
if (this.controller != null) {
controller.get_message("Error: unable to read from file at "+path);
}
}
boolean addOk=EventSet.getInstance().add(toAdd);
if (this.controller!=null) {
if (addOk) {
controller.get_message("Events added");
} else {
controller.get_message("No events to add");
}
}
} else if (line.cmd_check() == 2) {
boolean valid = line.validate_date_iterval(line.getIn_line());
if (valid) {
Date[] dates = line.convertIntervaltoDates(line.intervalExtract(line.getIn_line()));
createmap(dates[0], dates[1]);
if (this.controller != null) {
controller.get_message("Map correctly created for "+ line.getIn_line());
}
} else if (this.controller != null) {
controller.get_message("Invalid date at "+ line.getIn_line()+": unable to create map");
}
}
current = br.readLine();
}
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
添加回答
举报