So I have a text file as :
and I want to count the number of integers in the first row.
// e.g. The first row : 3 12 1 8 5 8 1 2 1 4 --> 10
Can I do that with a stream
or for
statement or another way?
I tried with for and it didn't work for me and I couldn't find any useful solution. Please, help me.
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;
}
}
}