【发布时间】:2019-01-03 17:01:56
【问题描述】:
所以我有一个文本文件:
我想计算第一行的整数个数。
// e.g. The first row : 3 12 1 8 5 8 1 2 1 4 --> 10
我可以使用stream 或for 语句或其他方式来做到这一点吗?
我尝试了 for 但它对我不起作用,我找不到任何有用的解决方案。请帮我。
public class Egyszamjatek {
public static void main(String[] args) throws IOException {
List<String> game = Files.readAllLines(Paths.get("egyszamjatek.txt"));
ArrayList<OneGame> games = new ArrayList<>();
for (String game1 : game) {
String[] split = game1.split(" ");
int rounds = Integer.parseInt(split[0]) + Integer.parseInt(split[1]) + Integer.parseInt(split[2])
+ Integer.parseInt(split[3]) + Integer.parseInt(split[4]) + Integer.parseInt(split[5])
+ Integer.parseInt(split[6]) + Integer.parseInt(split[7]) + Integer.parseInt(split[8])
+ Integer.parseInt(split[9]);
String names = split[10];
games.add(new OneGame(rounds, names));
}
System.out.println("3.feladat: number of players : " + game.stream().count());
System.out.println("4. feladat: number of rounds: " );
}
static class OneGame {
int rounds;
String names;
public OneGame(int rounds, String names) {
this.rounds = rounds;
this.names = names;
}
}
}
【问题讨论】:
-
您的解决方案面临的问题是什么?任何异常或没有数据?
-
问题是我不知道如何计算第一行的整数元素。
-
名称前的数字是四舍五入。所以我想数回合数,所以我需要数数。
标签: java list arraylist java-stream