1

It's a program that is a sports league, a group of teams plays through a Schedule of Games and determine the winner.

The program runs fine to the point where the final output won't let me see the winner, it shows this instead:

The season winner(s) with 9 points: [Ljava.lang.String;@e73f9ac

I changed it to teams.length which made the program work but it would show me the teams (i) number instead of the string name like "Vancouver".

Thanks in advance.

    }

    int peak = 0;                                
    int[] total = new int[teams.length];         

    for (int i=0; i<teams.length; i++){                    
      total[i] = 2*wins[i]+ties[i]; 
      if (total[i] > peak) peak = total[i];

      System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " losses, " + ties[i] + " ties = " + total[i]);
  }    

  System.out.println("The season winner(s) with " + peak + " points: " + teams);  

  for (int i=0; i<teams.length; i++){
    if (peak < total[i]) peak = total[i];
  }      

}

static int indexOfTeam(String team, String[] teams){  
  for (int i=0; i<teams.length; i++)
    if (team.compareTo(teams[i]) == 0) return i;
    return -1;
  }
}
Wietlol
  • 69
  • 1
  • 10

3 Answers3

1

Instead of printing the winning team you're printing the teams array.
While iterating store besides the peak, the index of the winning team:

 int index = -1;
 for (int i=0; i<teams.length; i++){                    
     total[i] = 2*wins[i]+ties[i]; 
     if (total[i] > peak) {
        index = i;
        peak = total[i];
     }
     System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " 
   losses, " + ties[i] + " ties = " + total[i]);
 } 

and finally:

System.out.println("The season winner(s) with " + peak + " points: " + 
 teams[index]);
forpas
  • 160,666
  • 10
  • 38
  • 76
0

teams is an array of Strings, you must insert the index after the name

0

If you want to print all teams use Arrays.toString(teams), but I think that you would like to print only part of teams array, so you can create list of winners

 List<String> winners = new ArrayList<Integer>;
 for (int i=0; i<teams.length; i++){                    
     total[i] = 2*wins[i]+ties[i]; 
     if (total[i] > peak) {
        winners.add(teams[i]);
        peak = total[i];
     }
 System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " 
  losses, " + ties[i] + " ties = " + total[i]);
} 

indexes object you can simply print

System.out.println("The season winner(s) with " + peak + " points: " +winners);

If you want to have an array you can use toArray() on winners object.

jker
  • 465
  • 3
  • 13