Alright, so basically I have this program going, it reads the text file perfectly, I also have some arrays created. Now I need it to store each team's rebounds, assists, and points separately. From there I need to get the mean, median, mode for each section (which will be completed after). The part which I'm stuck at is storing each team's specific rebounds, assists and points into the specified arrays.

Here is what the text file contains:

Player Team Points Rebounds Assists
Garnett-Kevin (Boston-Celtics) 23 15 3
Rondo-Rajon (Boston-Celtics) 6 51 6
Dooling-Keyon (Boston-Celtics) 54 23 7
Bass-Brandon (Boston-Celtics) 74 23 5
Allen-Ray (Boston-Celtics) 9 90 107
Johnson-Amir (Toronto-Raptors) 51 12 65
Bayless-Jerryd (Toronto-Raptors) 20 26 14
Bargnani-Andrea (Toronto-Raptors) 43 18 17
Calderon-Jose (Toronto-Raptors) 32 19 19
DeRozan-DeMar (Toronto-Raptors) 17 16 14

What I am thinking of doing is:

public static void main(String[] args) {
      int[] tDotPoints = new int[10];
           int [] tDotRebounds = new int[10];
           int [] tDotAssists = new int[10]; //arrays to hold the scores for each catergory of each player
           int [] bostonPoints = new int [10];
           int [] bostonRebounds = new int [10];
           int [] bostonAssists = new int [10];  

for (counter = 0; scan.hasNextInt; counter++){ //need to have it scan integers as they appear
if (counter % 3 = 0){  //for the points
add to this array using a method
}
if (coutner % 3 = 1){  //for the rebounds
add to that array using a a method
}
if (counter % 3 = 2){  //for the assists
add to that array using a method
}

I didn't post the code for reading the file, because that is already completed. However, I have posted what I think needs to be completed, and also the created arrays. I know it's a bit incorrect, but if someone can elaborate on how I can accurately complete this, then that would be greatly appreciated. Thanks!

Dani AI

Generated

Good approach: parse the file line-by-line, extract the team name and the three trailing integers from each player line, and collect those numbers into per-team containers instead of fixed-size arrays. That avoids brittle index math and lets you handle varying team sizes and messy spacing. already has the file read working; build on that by mapping each team to a small stats holder and appending values as you parse.

Example parsing + storage (robust to extra spaces and skips the header):

class TeamStats {
    List<Integer> points = new ArrayList<>();
    List<Integer> rebounds = new ArrayList<>();
    List<Integer> assists = new ArrayList<>();
}

Map<String, TeamStats> teams = new HashMap<>();
Pattern p = Pattern.compile("\\(([^)]+)\\)\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*$");

try (BufferedReader br = new BufferedReader(new FileReader("players.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        Matcher m = p.matcher(line);
        if (!m.find()) continue; // header or malformed line
        String team = m.group(1);
        int pts = Integer.parseInt(m.group(2));
        int reb = Integer.parseInt(m.group(3));
        int ast = Integer.parseInt(m.group(4));
        TeamStats ts = teams.computeIfAbsent(team, k -> new TeamStats());
        ts.points.add(pts);
        ts.rebounds.add(reb);
        ts.assists.add(ast);
    }
}

Simple helpers for mean / median / mode (apply per TeamStats list):

static double mean(List<Integer> L) { return L.stream().mapToInt(i->i).average().orElse(Double.NaN); }

static double median(List<Integer> L) {
    if (L.isEmpty()) return Double.NaN;
    List<Integer> s = new ArrayList<>(L);
    Collections.sort(s);
    int n = s.size();
    return (n % 2 == 1) ? s.get(n/2) : (s.get(n/2 - 1) + s.get(n/2)) / 2.0;
}

static List<Integer> mode(List<Integer> L) {
    if (L.isEmpty()) return Collections.emptyList();
    Map<Integer, Long> freq = L.stream().collect(Collectors.groupingBy(i->i, Collectors.counting()));
    long max = freq.values().stream().mapToLong(x->x).max().orElse(0);
    return freq.entrySet().stream().filter(e->e.getValue()==max).map(Map.Entry::getKey).collect(Collectors.toList());
}

Troubleshooting notes: ’s lastIndexOf(')') + split idea is workable, but the regex above is more tolerant of spacing. ’s note about using the correct comparison/operator is valid when you do conditional checks. Add defensive parsing (catch NumberFormatException, log and skip malformed lines) and prefer dynamic lists (ArrayList) rather than fixed arrays sized to 10 unless you know the exact count.

Recommended Answers

All 4 Replies

why are you using this

if (counter % 3 = 2){ }

i dont get the '% 3' and to be honest this must be the first time ive seen such an if statement

why are you using this

if (counter % 3 = 2){ }

i dont get the '% 3' and to be honest this must be the first time ive seen such an if statement

Haha, you must be new at programming, just like me :)

% is called modular in Java, and is used to get the remainder. For example, 10%3 = 1, this is because 3 goes into 10 three times, and is left with a remainder of one.

Haha, you must be new at programming, just like me :)

% is called modular in Java, and is used to get the remainder. For example, 10%3 = 1, this is because 3 goes into 10 three times, and is left with a remainder of one.

lol no im not that new, but why do you want to use that? its just compilcating stuff?

as you read in the value if its the first value after the teams name then it will be a rebound, if its the 2nd value after the team it will be an assist and so on? why not just use lastindexof(')'); and from there read in the string this should give you only the scores which are seperated by a white space, then split() them according to that white space and what you are left with is 3 values in a String or int array which can then be added to their specific arrays

why are you using this if (counter % 3 = 2){ } i dont get the '% 3' and to be honest this must be the first time ive seen such an if statement

Me too, but not because of the %, but because it's not Java. It won't compile with a Java compiler. You can't assign the value 2 to the integer constant 3.
All together now children, repeat after me:
= is assignment
== is the test for equality

:twisted: J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.