Hello, this is my first post!
I'm a CS student and I've been reading this server a lot to learn. 🙂
I was thinkering about this specific MOOC problem and I wasn't able to come up with another solution for this.
Each time the chosen team's name is found in a line, it sums up as game played (countGames).
Then, based on it's position on the line ([0] or [1]), it sum up in countWin or countLose.
Scanner scanner = new Scanner(System.in);
// File:
String fileName = scanner.nextLine();
int countGames = 0;
int countWin = 0;
int countLose = 0;
try(Scanner csvFile = new Scanner(Paths.get(fileName))){
// Chosen Team to search:
String inputTeam = scanner.nextLine();
while(csvFile.hasNextLine()){
String[] csvLine = csvFile.nextLine()
.split(",");
// Team x and y score indexes will always be i+numOfTeams;
String team1 = csvLine[0];
String team2 = csvLine[1];
int scoreTeam1 = Integer.parseInt(csvLine[2]);
int scoreTeam2 = Integer.parseInt(csvLine[3]);
if(team1.equals(inputTeam)){
countGames++;
if(scoreTeam1 > scoreTeam2){
countWin++;
}else {
countLose++;
}
}
if(team2.equals(inputTeam)){
countGames++;
if(scoreTeam2 > scoreTeam1){
countWin++;
}else{
countLose++;
}
}
}catch(Exception e){
...
}
csv example:
Foo,Bar,5,9
Baz,Foo,10,5
Baz,Bar,20,9
I feel like this solution is ugly.
Plus, if there were more than 2 options to search in each line, it would be even more nested and ugly. In that scenario I could use switch, but it feels like it's still not the best solution.
What do you guys think, and how would you guys deal with this problem in order to bring up a cleaner solution?
Thanks for reading, wish everyone a great day. ❤️

Hmm I see, so a