我需要为调度程序导入特殊格式的文本文件,因为时间压力非常高,我决定寻求帮助并在将来更好地学习它。文件格式如下所示:
2 0 1 3 5 4 t 1 3 6 7 3 6
1 2 4 5 0 3 t 8 5 10 10 10 4
2 3 5 0 1 4 t 5 4 8 9 1 7
1 0 2 3 4 5 t 5 5 5 3 8 9
2 1 4 5 0 3 t 9 3 5 4 3 1
1 3 5 0 4 2 t 3 3 9 10 4 1
每一行代表一项活动,t代表工人的数字和t代表工人后的数字。
到目前为止,我发现它看起来应该是这样的:
public static List<ArrayList<WorkUnit>> readInputFile(String fileName)
throws IOException {
InputStream inputStream;
if (fileName.equals("/textfile.txt"))
inputStream = Util.class.getResourceAsStream(fileName);
else
inputStream = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(
new InputStreamReader(new BufferedInputStream(inputStream), "UTF-8"));
String line;
String[] splitedLine;
List<ArrayList<WorkUnit>> activityLists =
new ArrayList<ArrayList<WorkUnit>>();
while ((line = reader.readLine()) != null) {
ArrayList<WorkUnit> activity = new ArrayList<WorkUnit>();
activity.add(new WorkUnit(activityId, workerId, duration));
activityLists.add(activity);
}
reader.close();
return activityLists;
}
我的想法是使用line.split(并且主要是我的问题)并在 for 循环中运行它,遗憾的是我失败了,如果你有任何想法如何在这或者不同的方式请帮忙。目标是获取每个activityId(行号从0开始)workerId(t之前的数字),为它们分配活动的持续时间(在文件中的t之后),并将它们添加到工作单位。
感谢任何帮助。 是的,我是个菜鸟。
-------------------------------------------- - - -编辑 - - - - - - - - - - - - - - - - - - - - - - ------
为WorkUnit和splitLine逻辑添加了逻辑,该逻辑与其他文本文件一起使用,如下所示: enter image description here
其中“:”之前的数字是activityIds,第一个数字是workerIds,第二个是持续时间
公共类WorkUnit实现了Comparable {
int activityId;
int workerId;
int duration;
public WorkUnit(int activityId, int workerId, int duration) {
super();
this.activityId = activityId;
this.workerId = workerId;
this.duration = duration;
}
public int getActivityId() {
return activityId;
}
public int getWorkerId() {
return workerId;
}
public int getDuration() {
return duration;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (workerId < 10) builder.append(" "); else builder.append(" ");
builder.append(workerId);
if (duration < 10) builder.append(" "); else builder.append(" ");
builder.append(duration);
return builder.toString();
}
@Override
public int compareTo(WorkUnit object) {
if (this.activityId == object.getActivityId()
&& this.duration == object.getDuration()
&& this.workerId == object.getWorkerId())
return 0;
if (this.duration > object.getDuration()) return 1;
return -1;
}
}
splitLine的逻辑:
while ((line = reader.readLine()) != null) {
ArrayList<WorkUnit> activity = new ArrayList<WorkUnit>();
//1st place is activity id; always ascending order 0 to n
splitedLine = line.split("(\\s)*(:)*\\s+");
int activityId = Integer.parseInt(splitedLine[0]);
for (int i = 1; i < splitedLine.length; i += 2) {
int workerId = Integer.parseInt(splitedLine[i]);
int duration = Integer.parseInt(splitedLine[i+1]);
activity.add(new WorkUnit(activityId, workerId, duration));
}
acrivityLists.add(activity);
}
答案 0 :(得分:0)
Line.split是个好主意。实现看起来像这样:
String[] splitedLine = line.split(" ");
作为参数,split()采用一个字符串来划分你的输入。然后,您可以使用以下方式访问元素:
splitedLine[0];
splitedLine[1]; etc
答案 1 :(得分:0)
在这里,我认为这就是你想要的,以便你的逻辑正常工作。
int activityNumber = 1;
while ((line = reader.readLine()) != null) {
ArrayList<WorkUnit> activity = new ArrayList<WorkUnit>();
//1st place is activity id; always ascending order 0 to n
splitedLine = line.split(" ");
// we know that value t will be the middle element.
int len = splitedLine.length;
for(int i=0; i<splitedLine.length/2; i++) {
int workerId = Integer.parseInt(splitedLine[i]);
int duration = Integer.parseInt(splitedLine[len - i - 1]);
activity.add(new WorkUnit(activityNumber, workerId, duration));
}
activityNumber++;
acrivityLists.add(activity);
}
希望这有帮助!