Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.nio.file.Paths;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class SportStatistics {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         int gamesPlayed = 0;
  10.         int wins = 0;
  11.         int losses = 0;
  12.  
  13.         System.out.println("File:");
  14.         String file = scan.nextLine();
  15.  
  16.         System.out.println("Team:");
  17.         String team = scan.nextLine();
  18.  
  19.         try ( Scanner readFile = new Scanner(Paths.get(file))) {
  20.             while (readFile.hasNextLine()) {
  21.                 String line = readFile.nextLine();
  22.                 String[] info = line.split(",");
  23.  
  24.                 String firstTeam = info[0];
  25.                 String secondTeam = info[1];
  26.                 int firstTeamPoints = Integer.valueOf(info[2]);
  27.                 int secondTeamPoints = Integer.valueOf(info[3]);
  28.  
  29.                 for (int i = 0; i < info.length; i++) {
  30.                     if (info[i].equals(team)) {
  31.                         gamesPlayed++;
  32.                         if (i == 0 && firstTeamPoints > secondTeamPoints) {
  33.                             wins++;
  34.                         } else if (i == 0) {
  35.                             losses++;
  36.                         }
  37.  
  38.                         if (i == 1 && firstTeamPoints > secondTeamPoints) {
  39.                             losses++;
  40.                         } else if (i == 1) {
  41.                             wins++;
  42.                         }
  43.                     }
  44.                 }
  45.  
  46.             }
  47.         } catch (Exception e) {
  48.             System.out.println("File does not exist.");
  49.         }
  50.  
  51.         System.out.println(
  52.                 "Games: " + gamesPlayed);
  53.         System.out.println(
  54.                 "Wins: " + wins);
  55.         System.out.println(
  56.                 "Losses: " + losses);
  57.  
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement