krambo krambo - 1 month ago 13
Java Question

Compare int values from objects in an array list, and then display the top 3 objects

I need to compare all objects in the ArrayList players, and have it return the top 3 players in terms of total points for the season (In the TopPoints method).

There are 5 players objects in the ArrayList, and their total points are: 379, 330, 313, 157, & 153. I know I need to compare them using TotalPts, but I'm drawing a blank.

Can anyone help guide me in the right direction?

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Roster {
static ArrayList <Player> players = new ArrayList<Player> ();
public static void main(String[] args) throws Exception {


File file = new File("C:\\Users\\tpurv_000\\Desktop\\Stats.txt");
Scanner scan = new Scanner(file);

scan.useDelimiter("\\r?\\n");
scan.nextLine(); // ignores first line, since it's a header

while(scan.hasNextLine()) {
String[] values = scan.nextLine().split("\t");
Player currentPlayer = new Player();
currentPlayer.setPlayerNum(Integer.parseInt(values[0]));
currentPlayer.setPlayerName(values[1]);
currentPlayer.setGP(Integer.parseInt(values[2]));
currentPlayer.setGS(Integer.parseInt(values[3]));
currentPlayer.setTotalMins(Integer.parseInt(values[4]));
currentPlayer.setTotalFG(Integer.parseInt(values[5]));
currentPlayer.setTotalFGA(Integer.parseInt(values[6]));
currentPlayer.setThreeFG(Integer.parseInt(values[7]));
currentPlayer.setThreeFGA(Integer.parseInt(values[8]));
currentPlayer.setFT(Integer.parseInt(values[9]));
currentPlayer.setFTA(Integer.parseInt(values[10]));
currentPlayer.setOffReb(Integer.parseInt(values[11]));
currentPlayer.setDefReb(Integer.parseInt(values[12]));
currentPlayer.setPF(Integer.parseInt(values[13]));
currentPlayer.setA(Integer.parseInt(values[14]));
currentPlayer.setTO(Integer.parseInt(values[15]));
currentPlayer.setSTL(Integer.parseInt(values[16]));
currentPlayer.setBLK(Integer.parseInt(values[17]));
currentPlayer.setTotalPts(Integer.parseInt(values[18]));
players.add(currentPlayer);
}
System.out.println(players);
TopPoints();

}

public static void TopPoints(){
int largest = 0;
int secondLargest = 0;
int thirdLargest = 0;


//Not sure how to compare




System.out.println(largest);

}


}

class Player {
private int playerNum, GP, GS, totalMins, totalFG, totalFGA, threeFG, threeFGA, FT, FTA, offReb, defReb, PF, A, TO, STL, BLK, totalPts;
private String playerName;


//accessors & mutators
public int getPlayerNum() {
return playerNum;
}

public void setPlayerNum(int playerNum) {
this.playerNum = playerNum;
}

public String getPlayerName() {
return playerName;
}

public void setPlayerName(String playerName) {
this.playerName = playerName;
}

public int getGP() {
return GP;
}

public void setGP(int gP) {
GP = gP;
}

public int getGS() {
return GS;
}

public void setGS(int gS) {
GS = gS;
}

public int getTotalMins() {
return totalMins;
}

public void setTotalMins(int totalMins) {
this.totalMins = totalMins;
}

public int getTotalFG() {
return totalFG;
}

public void setTotalFG(int totalFG) {
this.totalFG = totalFG;
}

public int getTotalFGA() {
return totalFGA;
}

public void setTotalFGA(int totalFGA) {
this.totalFGA = totalFGA;
}

public int getThreeFG() {
return threeFG;
}

public void setThreeFG(int threeFG) {
this.threeFG = threeFG;
}

public int getThreeFGA() {
return threeFGA;
}

public void setThreeFGA(int threeFGA) {
this.threeFGA = threeFGA;
}

public int getFT() {
return FT;
}

public void setFT(int fT) {
FT = fT;
}

public int getFTA() {
return FTA;
}

public void setFTA(int fTA) {
FTA = fTA;
}

public int getOffReb() {
return offReb;
}

public void setOffReb(int offReb) {
this.offReb = offReb;
}

public int getDefReb() {
return defReb;
}

public void setDefReb(int defReb) {
this.defReb = defReb;
}

public int getPF() {
return PF;
}

public void setPF(int pF) {
PF = pF;
}

public int getA() {
return A;
}

public void setA(int a) {
A = a;
}

public int getTO() {
return TO;
}

public void setTO(int tO) {
TO = tO;
}

public int getSTL() {
return STL;
}

public void setSTL(int sTL) {
STL = sTL;
}

public int getBLK() {
return BLK;
}

public void setBLK(int bLK) {
BLK = bLK;
}

public int getTotalPts() {
return totalPts;
}

public void setTotalPts(int totalPts) {
this.totalPts = totalPts;
}

//Computing methods
public int avgMin() {
return (this.totalMins / this.GP);
}

public int fgPercentage() {
return (this.totalFG / this.totalFGA) * 100;
}

public int threefgPercentage() {
return (this.threeFG / this.threeFGA) * 100;
}

public int freethrowPercentage() {
return (this.FT / this.FTA) * 100;
}

public int avgRebounds() {
return (this.offReb + this.defReb) / 2;
}
}

Answer

There is a lot of solutions for it. You can use TreeList, for example, to sort all item at moment that it is added.

To sort an ArrayList, you should use Collections.sort() method. Take a look in this example:

public class ArrayListSortExample{
    public static void main(String ... args){
        List<Player> players = new ArrayList<>();

        // Lets create 50 players with random score.
        Random random = new Random();
        for(int i = 0; i < 50; i++){
            Player player = new Player();
            player.setScore(random.nextInt(100));
            players.add(player);
        }

        // Lets sort players by score
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getScore() - o2.getScore();
            }
        });

        // Print 3 top players
        for(int i = 0; i < 3; i++){
            System.out.println((i+1) + "º place: " + players.get(i).getScore() + " score");
        }
    }

    public static class Player{
        private int score;

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }
    }
}
Comments