Search code examples
javaarraysio

Comparing Strings and Integers in an Array at the same time


I am doing an exercise that asks me to read a file in csv format, and ask user`s input about the word he wants the program to look for. This is the format of the document, index 0 and 1 are name of teams therefore Strings and index 2 and 3 are the scores of the games:

ENCE,Vitality,9,16
ENCE,Vitality,16,12
ENCE,Vitality,9,16
ENCE,Heroic,10,16
SJ,ENCE,0,16
SJ,ENCE,3,16
FURIA,NRG,7,16
FURIA,Prospects,16,1

At first the exercise asked me to write a program that reads the document and print how many games certain team played. Now it wants me to write one that will compare scores and print the total number of wins and losses of that specific team. I tried to do it in a million different ways, is there an effective way to compare Strings and integers at the same time ?

My code below:

import java.nio.file.Paths;

import java.util.Scanner;

public class SportStatistics {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("File:");
        String file = scan.nextLine();

        System.out.println("Team:");
        String team = scan.nextLine();

        try ( Scanner reader = new Scanner(Paths.get(file))) {
            int totalGames = 0;
            int teamPoints = 0;
            int otherPoints = 0;
            int wins = 0;
            int losses = 0;

            while (reader.hasNextLine()) {

                String info = reader.nextLine();

                if (info.isEmpty()) {
                    continue;
                }
                String[] parts = info.split(",");
                String homeN = parts[0];
                String visitorN = parts[1];
                int homeP = Integer.valueOf(parts[2]);
                int visitorP = Integer.valueOf(parts[3]);

                for (String part : parts) {
                    if (part.equals(team)) {
                        totalGames++;
                        
                    }
                    
                    if(homeN.equals(team)){
                        teamPoints = homeP;
                        otherPoints = visitorP;
                         if(teamPoints > otherPoints){
                            wins ++;
                        }else{
                             losses ++;
                         }
                        
                    }
                    
                    if(visitorN.equals(team)){
                        teamPoints = visitorP;
                        otherPoints = homeP;
                         if(teamPoints > otherPoints){
                            wins ++;
                        }else{
                             losses ++;
                         }
                        
                    }

                }

            }
            System.out.println("Games: " + totalGames);
            System.out.println(wins);
            
        } catch (Exception e) {

        }

    }
}


Solution

  • (This is the kind of problem where using a database would make more sense)

    So first, what you want is a Game class

    public class Game {
      String team1;
      String team2;
      int team1Score, team2Score;
    
      public Game(String t1, String t2, int score1, int score2);
    }
    

    Now you want to make a collection (probably a list) of games

    List<Game> games = new ArrayList<>();
    

    Then you want to add all the games to the list

    Then you have a choice. You could create a Map<String, List> and create lists for all your teams that include all the games they played. Or you could make a method, like

    public List<Game> getGamesForTeam(String teamName, List allGames) {
      ...
    }
    

    That extracts a list of games that included team teamName

    Hopefully this makes sense to you and will get you started