d.glez
d.glez

Reputation: 21

Java Arrays: Sorting scores and displaying with proper name

I want to be able to sort this arrays from highest to lowest with their corresponding names. I have created a method to help me short the array scores from lowest to highest but I am having trouble finding a way to link the name and score together to be able to print out together. (I have not called my method for this same reason)

import java.util.*;

/**
 * This program will prompt the user to enter the number of game players,
 * the game players' names, and their scores, and prints game players name
 * and scores in decreasing order of their scores. 
 *
 */
//class name
public class SortGameScores 
{
    // main program start 
    public static void main(String[] args)
    {
        //declaring variable 
        int  numberOfPlayers;

        //create new scanner 
        Scanner input = new Scanner(System.in);

        //Ask user to enter number of players and record number 
        System.out.print("Enter number of players: ");
        numberOfPlayers = input.nextInt();

        //Create and declare arrays using previous input 
        int[] player = new int [numberOfPlayers];
        String[] name = new String[numberOfPlayers];
        int[] score = new int [numberOfPlayers];

        //Ask user to enter each player's name 
        for (int i = 0; i < player.length; i++)
        {
            System.out.print("Enter player's name: ");
            name[i] = input.next(); 
        }

        //Ask user to enter each player's score
        for (int i = 0; i < player.length; i++)
        {
        System.out.print("Enter player's score: ");
        score[i] = input.nextInt();

        }

        //Print name and score for each array. 
        for (int i = 0; i < numberOfPlayers; i++)
        {
            System.out.println("Player: " + name [i] + " , " + "score: " + score [i]);

        }
    }//Close Main

    //method that will assort each score lowest to highest 
    public static void selectionSort(int[] list)
    {
        for (int i = 0; i < list.length - 1; i++) 
        {
            int currentMin = list[i];
            int currentMinIndex = i; 


            for (int j = i + 1; j < list.length; j++) 
            {
                if (currentMin > list[j]) 
                {
                    currentMin = list[j];
                    currentMinIndex = j;
                }
            }
            if (currentMinIndex != i)
            {
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }

    }//Close method selectionSort
}//close Class

Upvotes: 0

Views: 7740

Answers (2)

ericbn
ericbn

Reputation: 10958

You better describe your data with a class:

public class Player {
    private String name;
    private int score;

    public Player(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
}

And sort the elements in a Player[] using a Comparator<Player>:

class ByScoreComparator implements Comparator<Player> {
    @Override public int compare(Player p1, Player p2) {
        return Integer.compare(p1.getScore(), p2.getScore());
    }
}

// ...

Arrays.sort(players, new ByScoreComparator());

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

The problem is that you're using two parallel arrays, instead of using a single one, containing objects.

Create a class Player, with a name and a score fields, create an array of Players, and sort the players by their score (using a Comparator<Player>).

Upvotes: 1

Related Questions