0

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.

Audiosleef
  • 137
  • 3
  • 14

1 Answers1

1

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.

miroxlav
  • 11,796
  • 5
  • 58
  • 99
  • I'm afraid i'm not really seeing it, I was able to add the correct number with the nested for loop, but then I did not have an index to get the correct values from my list. – Audiosleef Jul 15 '17 at 15:11
  • @Audiosleef – regarding team numbers, your question is a bit unclear. Do you want to have team numbers 1, 2, 1, 2, etc. or 1, 2, 3, ..., 64? It would be the best if you could create a short sample CSV with expected results and insert in into the question. – miroxlav Jul 15 '17 at 15:39
  • I'm sorry if I was being unclear, I modified to post with additional information. – Audiosleef Jul 15 '17 at 16:22
  • @Audiosleef – I think my answer should now cover your both questions. If anything is unclear, please ask me. – miroxlav Jul 15 '17 at 16:47
  • Yes, but which number do I use then as an index for the .get() method on the spelers list? – Audiosleef Jul 15 '17 at 16:54
  • `(i - 1) * 14 + j`. And your third CSV column won't be getting value from `spelers` again, but getting a role of the player, if I understand end of your question correctly. – miroxlav Jul 15 '17 at 18:01
  • your answer (i - 1) * 14 + j did the trick, the second spelers.get() was unneccessary. I'll give the roles a shot tomorrow but I think i'll implement a switch statement in the inner for loop that should do the trick. Thank you for your help ! – Audiosleef Jul 15 '17 at 19:12
  • @Audiosleef – Java does not have [ranges in `switch` statement](https://stackoverflow.com/questions/10873590/in-java-using-switch-statement-with-a-range-of-value-in-each-case) so maybe roles will be easier using `if` or using `?:` operator. – miroxlav Jul 15 '17 at 19:51