【发布时间】:2019-05-14 22:47:17
【问题描述】:
已经这样做了,但不能让它工作。
还尝试创建另一个 while ((line = br.readLine()) != null) {},并将排序放在它之前,但它不会读取这个 while,所以它不会打印任何东西。
文件如下所示:
1-Fred-18-5-0
2-luis-12-33-0
3-Helder-23-10-0
并希望它像这样打印:
2-luis-12-33-0
3-Helder-23-10-0
1-Fred-18-5-0
public static void lerRanking() throws IOException {
File ficheiro = new File("jogadores.txt");
BufferedReader br = new BufferedReader(new FileReader(ficheiro));
List<Integer> jGanhos = new ArrayList<Integer>();
int i = 0;
String line;
String texto = "";
while ((line = br.readLine()) != null) {
String[] col = line.split("-");
int colunas = Integer.parseInt(col[3]);
jGanhos.add(colunas);
i++;
if(i>=jGanhos.size()){
Collections.sort(jGanhos);
Collections.reverse(jGanhos);
for (int j = 0; j < jGanhos.size(); j++) {
if(colunas == jGanhos.get(i)){
texto = texto + line + "\n";
}
}
}
}
PL(texto);
}
【问题讨论】:
-
你能给我们一个输入示例吗?
-
已经做了,不知道大家能不能看懂。
-
如果您为您的数据创建一个自定义类会更好,这样您就可以将值保持在一起。
标签: java file sorting columnsorting