【发布时间】:2018-01-09 20:01:20
【问题描述】:
我需要为调度器导入特殊格式的文本文件,时间压力真的很大,我决定寻求帮助,以后更好地学习它。文件格式如下:
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。
感谢您的帮助。 是的,我是菜鸟。
------------------------------------------ - - -编辑 - - - - - - - - - - - - - - - - - - - - - - ------
为 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);
}
【问题讨论】:
-
分割逻辑发生在代码的什么地方?我只是看到你读了一行,然后为 WorkUnit 创建了一个新对象,然后结束了
-
还分享你的 WorkUnit 结构......它由什么组成......一旦你向我提供所有这些信息,我可以帮助你解决这个问题。
-
@zenwraight 已更新,希望这些是正确的信息。非常感谢您的帮助,我认为我的问题在于在检测到新行时创建 acitvityId,并正确拆分字符串以获取所有信息
-
所以你只是在拆分方法中遇到了麻烦?还是你的主要逻辑也有问题?
-
@zenwraight 我认为拆分方法很麻烦,我想我应该能够处理其余的,但我不能 100% 确定,因为经过多次尝试后我无法运行该应用程序,一切都表明问题出在拆分字符串逻辑中:)