import java.util.Scanner;
import java.nio.file.Paths;
public class Recap {
public static void main(String[] args) {
int counter = 0;
int winCounter = 0;
int lossCounter = 0;
System.out.println("File:");
Scanner input = new Scanner(System.in);
String file = input.nextLine();
System.out.println("Team:");
String team = input.nextLine();
// Read file
try(Scanner sc = new Scanner(Paths.get(file))) {
while(sc.hasNextLine()) {
String line = sc.nextLine();
if (line.contains(team)) {
counter++;
int positionOfTeam = line.indexOf(team);
String[] parts = line.split(",");
if (positionOfTeam == 0){
int numOfPoints = Integer.parseInt(parts[2]);
int numOfPointsOtherTeam = Integer.parseInt(parts[3]);
if (numOfPoints > numOfPointsOtherTeam ) {
winCounter++;
}
else {
lossCounter++;
}
}
else {
int numOfPoints = Integer.parseInt(parts[3]);
int numOfPointsOtherTeam = Integer.parseInt(parts[2]);
if (numOfPoints > numOfPointsOtherTeam ) {
winCounter++;
}
else {
lossCounter++;
}
}
}
}
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println("Games: " + counter);
System.out.println("Wins: " + winCounter);
System.out.println("Losses: " + lossCounter);
}
}
#Code Review for reading from a file - final exercise in part 4 MOOC reading from file
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
Formatted code
import java.util.Scanner;
import java.nio.file.Paths;
public class Recap {
public static void main(String[] args) {
int counter = 0;
int winCounter = 0;
int lossCounter = 0;
System.out.println("File:");
Scanner input = new Scanner(System.in);
String file = input.nextLine();
System.out.println("Team:");
String team = input.nextLine();
// Read file
try (Scanner sc = new Scanner(Paths.get(file))) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.contains(team)) {
counter++;
int positionOfTeam = line.indexOf(team);
String[] parts = line.split(",");
if (positionOfTeam == 0) {
int numOfPoints = Integer.parseInt(parts[2] );
int numOfPointsOtherTeam = Integer.parseInt(parts[3] );
if (numOfPoints > numOfPointsOtherTeam) {
winCounter++;
}
else {
lossCounter++;
}
}
else {
int numOfPoints = Integer.parseInt(parts[3] );
int numOfPointsOtherTeam = Integer.parseInt(parts[2] );
if (numOfPoints > numOfPointsOtherTeam) {
winCounter++;
}
else {
lossCounter++;
}
}
}
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println("Games: " + counter);
System.out.println("Wins: " + winCounter);
System.out.println("Losses: " + lossCounter);
}
}
<@&987246399047479336> please have a look, thanks.
Hi guys, I was just tackling this last exercise on Part 4 in the MOOC first part on reading file. I don't know where to find the exercise template, so I just try to implement the code by myself. The code works but I'm sure this can be made simpler; I uses duplicated lines, there should be an "easier" way I guess. Can someone review the code and comment on it by giving feedbacks please.
Anyone to review the code pls :c
Unfortunately they're specific about the sequence of events...
Write a program that prompts the user for a filename,
after which it reads the match statistics from the file.
The program then prompts the user for the name of a team,
and prints the data specified in the following parts for that team.
The exercise is easier if you get the team name before reading the file (because then you only need to consider games in which this team played, but the exercise doesn't ask for that (though it would have needed to be quite a bit smarter to test that you are following that rule).
But considering the approach you've taken...
Using line.contains(team) only works with these team names. It is possible to have a team name that is a prefix or substring of another team - in which case the match would be a false positive. Or the user could type only a fragment, and get false positives. Really you should split first, then check if the name matches either of the first two fields.
This would mean you wouldn't use indexOf to find the position either.
Technically you don't need counter since it's the sum of winCounter and lossCounter.
You could move the parsing of the points outside of the if statement so it's done once.
int firstTempPoints = Integer.parseInt(parts[2]);
int secondTeamPoints = Integer.parseInt(parts[3]);
if (positionOfTeam == 0){
if (firstTempPoints > secondTeamPoints) {
winCounter++;
}
else {
lossCounter++;
}
}
else {
if (secondTeamPoints > firstTempPoints) {
winCounter++;
}
else {
lossCounter++;
}
}
A correct implementation would instead read the file into a list of games, then ask for the team, and do this tallying from the list of games. The exercise doesn't require that the content is parsed into Game objects... but I expect that is the intention.