Audiosleef
Audiosleef

Reputation: 137

Adding sequence to a csv file

I have created a csv file with randomnly generated player names. Now I have to add these players to a team by appending the team ID to their names. Each team consists of 14 players so I have to loop through these names and add a 1 to the first 14 names, followed by a 2 for the next 14 names. This is what I have so far:

BufferedReader br = null;
BufferedReader br2 = null;
String line = "";
String cvsSplitBy = ",";
List<String> spelers = new ArrayList<>();

try {
    br = new BufferedReader(new FileReader("spelers.csv"));
    try {
        while((line = br.readLine()) != null){
            String[] speler = line.split(",");
            spelers.add(speler[1] + "," + speler[2]);
        }

        File file = new File("\\test.csv");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);
        for (int i = 1; i < 65; i++) {
            for (int j = 0; j < 14 ; j++) {
                System.out.println(i + "," + spelers.get(?) + "," + spelers.get(?));
            }
        }

After that, I have another issue where I have to append "Captain" after the first of every 14 players, "Starter" after the next 10 and "Reserve" after the remaining 3.

Edit: For clarification:

There are 896 players. There are 64 teams. Each team has 14 players

1,Klaas,Dembele
1,Jonas,Naingollan
1,Wesley,Vertonghen
1,Bart,Lukaku
1,Mattias,Carrasco
1,Giovanni,Vertonghen
1,Bart,Naingollan
1,Wesley,Dembele
1,Olivier,Dembele
1,Bart,Alderweireld
1,Bart,Dembele
1,Giovanni,Carrasco
1,Sander,Naingollan
1,Klaas,Dembele
2,Klaas,Mertens
2,Ward,Vermaelen
2,Dane,Lukaku
2,Giovanni,Carrasco
2,Klaas,Vermaelen
2,Giovanni,Lukaku
2,Jonas,Vertonghen
2,Klaas,Lukaku
2,Wesley,Vertonghen
2,Mattias,Mertens
2,Giovanni,Carrasco
2,Klaas,Naingollan
2,Mattias,Naingollan
3,Sander,Dembele
3,Dane,Lukaku

These are the actual names that I generated. The first 14 player in the list are assigned to team 1, the next 14 to team 2, etc.

Upvotes: 0

Views: 332

Answers (1)

miroxlav
miroxlav

Reputation: 12194

Team number:

In case if you want team numbers 1, 2, ..., 64, just keep the i, which then already contains team number.

Player name from names list:

Use spelers.get((i - 1) * 14 + j)

Player role:

just create the role based on j:

j == 0 → "Captain"
j <= 10 → "Starter"
otherwise → "Reserve"

hopefully this gives an idea and you can easily convert it into Java code. If not, please let me know.

Upvotes: 1

Related Questions