Trying to sort Array<int, String>

Discussion in 'Programming' started by ddc, Dec 10, 2010.

  1. #1
    I'm new to the forum and am trying to sort a custom Class Object array in descending order. First, lexicographically based on the String. Second, numerically based on the integer. I am having trouble getting it to sort lexicographically and haven't gotten around to sorting it numerically yet because I am having problems getting the program to output properly. I believe the error lies around line 84. I also copied and pasted the code from the separate class file that holds the Player object and placed it below the main program code. Any help would be greatly appreciated!


    import java.io.*;
    import java.util.*;


    public class ProjectFive
    {
    public static void main(String[] args) throws IOException
    {
    final int NUM_PLAYERS = 6;
    String string1 = "Player", string2 = "Jersey Number";


    //TODO Initialize array of objects
    Player[] p = new Player[NUM_PLAYERS];


    File file1 = new File("Team.txt");
    Scanner inFile = new Scanner(file1);


    //TODO Read data from file into array
    for(int i = 0; i < p.length; i++)
    {
    p = new Player(inFile.nextInt(), inFile.nextLine().trim());
    }

    inFile.close();

    System.out.printf("%10s%20s\n", string1, string2);


    //TODO List data in array (unsorted)
    for(int i = 0; i < p.length; i++)
    {
    System.out.printf("%10s", p.getPlayerName());
    System.out.printf("%20d\n", p.getPlayerNum());
    }


    //TODO Sort data alphabetically
    int startScan, index, minIndex;
    // minValue temporarily holds the smallest
    // element in the array - needed for swap below
    // minValue will always have the same type as
    // each member of the array
    String minValue;

    // The first time through, look at the array starting at
    // the beginning. Swap the smallest value with the
    // starting value.
    // Each time after that, look at the array starting at
    // the next index, because you know that the smallest
    // value(s) is/are already sorted at the beginning
    // of the array.
    for (startScan = 0; startScan < (p.length-1); startScan++)
    {

    // At the start, the minimum value is just
    // the first value in the array.
    minValue = p[startScan].getPlayerName();
    // For now, the minIndex is the index that we're
    // starting at.
    minIndex = startScan;


    for(index = startScan + 1; index < p.length; index++)
    {
    // Compare and find the smallest value
    if (p[index].getPlayerName().compareTo(minValue) < 0)
    {
    // The array element with the minimum value
    // is saved in the minValue variable
    minValue = p[index].getPlayerName();
    // Remember where the minimum value is
    minIndex = index;
    }
    }
    // Put the value from the front of the array
    // into the place where the minimum value is
    //p[minIndex] = new Player(p[startScan].getPlayerNum(), p[startScan].getPlayerName());
    p[minIndex] = p[startScan];
    // Put the minimum value at the front
    // of the array
    p[startScan] = new Player(p[minIndex].getPlayerNum(), minValue);
    }

    System.out.println();


    System.out.printf("%10s%20s\n", string1, string2);


    //TODO List data in array (now sorted alphabetically)
    for(int i = 0; i < p.length; i++)
    {
    System.out.printf("%10s", p.getPlayerName());
    System.out.printf("%20d\n", p.getPlayerNum());
    }

    //TODO Sort data numerically


    //TODO List data in array (now sorted numerically)


    }
    }




    public class Player
    {
    //Private data fields
    private int playerNum;
    private String playerName;

    //Constructor. This is the only place to write to data fields
    public Player(int a, String b)
    {
    playerNum = a;
    playerName = b;
    }

    //Accessor for Player Number
    public int getPlayerNum()
    {
    return playerNum;
    }

    //Accessor for Player Name
    public String getPlayerName()
    {
    return playerName;
    }
    }
     

    Attached Files:

    • Team.txt
      File size:
      61 bytes
      Views:
      54
    ddc, Dec 10, 2010 IP