How do I find the maximum value among multiple arrays and return the value of the first index in java?

Viewed 53

My code prints out a list of different gamers and I want to find which list has the highest total value. I want the name of the gamer to be printed underneath the table values in the line Top: Gamer.

public static void main(String[] args) throws IOException {

        File gameScores = new File("C:\\Users\\jaret\\OneDrive\\Desktop\\GameScores.csv");
        if (gameScores.exists()) {
            BufferedReader br = null;
            String line = "";
            String csvSplitBy = ",";
            int gamerCount = 0;
            br = new BufferedReader(new FileReader(gameScores));
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Games Report");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Gamer   1   2   3   4   5   6   7");
            System.out.println("    8   9   10  Total");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");

            while ((line = br.readLine()) != null) {
                String list[] = new String[10];
                list = line.split(csvSplitBy);
                int sum = 0;

                for (String element : list) {
                    try {
                        Integer num = Integer.parseInt(element);
                        sum += num;
                    } catch (NumberFormatException nfe) {
                        System.out.print(list[0] + "\t" + list[1] + (list[1].length() > 10 ? "\t" : "\t") + list[2]
                                + (list[2].length() > 10 ? "\t" : "\t") + list[3]
                                + (list[3].length() > 10 ? "\t" : "\t") + list[4]
                                + (list[4].length() > 10 ? "\t" : "\t") + list[5]
                                + (list[5].length() > 10 ? "\t" : "\t") + list[6]
                                + (list[6].length() > 10 ? "\t" : "\t") + list[7]
                                + (list[7].length() > 10 ? "\n\t" : "\n\t") + list[8]
                                + (list[8].length() > 10 ? "\t" : "\t") + list[9]
                                + (list[9].length() > 10 ? "\t" : "\t") + list[10] + "\t");
                    }
                }
                System.out.println(sum);
                gamerCount++;
            }

            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("# of Gamers:    " + gamerCount);
            System.out.println("Top Gamer:  ");
            br.close();
        }

        String newGamer;
        newGamer = userInput();
        enterGamer(newGamer);
    }

Here is my output:

----------------------------------------------------------
-------------------------------------
Games Report
----------------------------------------------------------
-------------------------------------
Gamer   1   2   3   4   5   6   7
    8   9   10  Total
----------------------------------------------------------
-------------------------------------
Bob 167 123 159 102 102 189 183
    173 197 148 1543
Sally   189 130 138 113 159 116 134
    196 150 144 1469
Mario   104 106 120 188 143 189 149
    174 163 100 1436
Lev 152 159 195 140 154 176 107
    128 166 181 1558
Carden  158 200 175 114 117 150 176
    181 131 132 1534
Adelie  175 199 122 104 198 182 175
    153 120 165 1593
Lada    161 108 102 193 151 197 115
    137 126 186 1476
Xavier  178 171 147 113 107 129 128
    189 165 195 1522
Raffi   176 144 151 124 149 112 158
    159 119 177 1469
Chang   135 144 177 153 143 125 145
    140 117 158 1437
Mich    156 105 178 137 165 180 128
    115 139 157 1460
Mason   162 185 108 106 113 135 139
    135 197 160 1440
Cora    186 115 106 126 135 108 157
    156 187 120 1396
Sergio  117 105 115 116 193 200 176
    134 122 153 1431
Jonas   132 163 196 101 134 159 131
    104 135 168 1423
----------------------------------------------------------
-------------------------------------
# of Gamers:    15
Top Gamer:
1 Answers

You need to keep track of the top score and also the name of the game with that top score. The following should work:

public static void main(String[] args) throws IOException {

    File gameScores = new File("C:\\Users\\jaret\\OneDrive\\Desktop\\GameScores.csv");
    if (gameScores.exists()) {
        BufferedReader br = null;
        String line = "";
        String csvSplitBy = ",";
        int gamerCount = 0;
        int topScore = 0;
        String topScorer = "";
        br = new BufferedReader(new FileReader(gameScores));
        System.out.println("----------------------------------------------------------");
        System.out.println("-------------------------------------");
        System.out.println("Games Report");
        System.out.println("----------------------------------------------------------");
        System.out.println("-------------------------------------");
        System.out.println("Gamer   1   2   3   4   5   6   7");
        System.out.println("    8   9   10  Total");
        System.out.println("----------------------------------------------------------");
        System.out.println("-------------------------------------");

        while ((line = br.readLine()) != null) {
            String list[] = new String[10];
            list = line.split(csvSplitBy);
            int sum = 0;

            for (String element : list) {
                try {
                    Integer num = Integer.parseInt(element);
                    sum += num;
                } catch (NumberFormatException nfe) {
                    System.out.print(list[0] + "\t" + list[1] + (list[1].length() > 10 ? "\t" : "\t") + list[2]
                            + (list[2].length() > 10 ? "\t" : "\t") + list[3]
                            + (list[3].length() > 10 ? "\t" : "\t") + list[4]
                            + (list[4].length() > 10 ? "\t" : "\t") + list[5]
                            + (list[5].length() > 10 ? "\t" : "\t") + list[6]
                            + (list[6].length() > 10 ? "\t" : "\t") + list[7]
                            + (list[7].length() > 10 ? "\n\t" : "\n\t") + list[8]
                            + (list[8].length() > 10 ? "\t" : "\t") + list[9]
                            + (list[9].length() > 10 ? "\t" : "\t") + list[10] + "\t");
                }
            }
            if (sum > topScore) {
                topScore = sum;
                topScorer = list[0];
            }
            
            System.out.println(sum);
            gamerCount++;
        }

        System.out.println("----------------------------------------------------------");
        System.out.println("-------------------------------------");
        System.out.println("# of Gamers:    " + gamerCount);
        System.out.println("Top Gamer:  " + topScorer);
        br.close();
    }

    String newGamer;
    newGamer = userInput();
    enterGamer(newGamer);
}
Related