Need a way to associate strings with individual array elements

user11973068 :

I am a complete beginner in programming and I'm working on a program for my mother that tracks her employee's monetary intake through a "horse race", with each employee having a horse and the program tracking their input to a UI made to look like a racetrack. After the help from my last inquiry, I've greatly simplified my mess of code but I am now faced with a new problem in that, after sorting the values largest to smallest, I have no way of associating the sorted values with the correct horse. I understand this explanation is confusing so I hope my code will do most of the talking for me here.

I honestly have no idea where to start with this. As I said in my last inquiry, I'm a complete beginner and severely lack the terminology or knowledge to find an answer here.

public class HorseRace {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String horse1 = "#5 Gitty-Up";
        String horse2 = "#7 Lady Simmons";
        String horse3 = "#6 Burning Peanutbutter";
        String horse4 = "#10 White Lightning";
        String horse5 = "#3 Bella";
        String horse6 = "#1 Meg The Stallion";
        float h1val;
        float h2val;
        float h3val;
        float h4val;
        float h5val;
        float h6val;
        System.out.println("Input amount for " + horse1 + ":");
        h1val = sc.nextFloat();
        System.out.println("Input amount for " + horse2 + ":");
        h2val = sc.nextFloat();
        System.out.println("Input amount for " + horse3 + ":");
        h3val = sc.nextFloat();
        System.out.println("Input amount for " + horse4 + ":");
        h4val = sc.nextFloat();
        System.out.println("Input amount for " + horse5 + ":");
        h5val = sc.nextFloat();
        System.out.println("Input amount for " + horse6 + ":");
        h6val = sc.nextFloat();
        Float[] values = new Float[]{h1val, h2val, h3val, h4val, h5val, h6val};
        Arrays.sort(values, Collections.reverseOrder());
        //currently displays horses with the wrong number. Need a way to tie the horse name strings to their respective float elements
        System.out.println("The current race progress is :");
             System.out.println(horse1 + " with $" + values[0]);
             System.out.println(horse2 + " with $" + values[1]);
             System.out.println(horse3 + " with $" + values[2]);
             System.out.println(horse4 + " with $" + values[3]);
             System.out.println(horse5 + " with $" + values[4]);
             System.out.println(horse6 + " with $" + values[5]);
    }
}

my desired result is printing the correct horse with the correct value. For example, if I put that #5 brought in $11 and #7 brought in $14, the program would print that #7 is in the lead with $14 and #5 is in second place with $11.

Currently, the program always prints #5 as being in the lead with the highest value, #7 being in second with the second highest, etc.

I understand this is because I am hard calling the horse1-horse6 values meaning they don't change, but these are acting more as placeholders while I figure out how to associate the right horse with the right value

Elliott Frisch :

Step 1, create a Horse class. It should have two fields, amount and name. It should implement Comparable because you want to sort it. And looking at your desired output, I would override toString().

class Horse implements Comparable<Horse> {
    private String name;
    private float amount;

    public Horse(String name, float amount) {
        this.name = name;
        this.amount = amount;
    }

    @Override
    public String toString() {
        return String.format("%s with $%.2f", name, amount);
    }

    @Override
    public int compareTo(Horse o) {
        return Comparator.comparing((Horse h) -> h.amount)
                    .thenComparing((Horse h) -> h.name).compare(this, o);
    }
}

Step 2, create an array of horseNames and iterate that populating an array of Horses (with amounts). Then sort it, and I would prefer Comparator.reverseOrder() to Collection.reverseOrder() when sorting an array.

public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    String[] horseNames = { "#5 Gitty-Up", "#7 Lady Simmons", 
            "#6 Burning Peanutbutter", "#10 White Lightning",
            "#3 Bella", "#1 Meg The Stallion" };
    Horse[] horses = new Horse[horseNames.length];
    for (int i = 0; i < horseNames.length; i++) {
        System.out.printf("Input amount for %s:%n", horseNames[i]);
        float amt = sc.nextFloat();
        horses[i] = new Horse(horseNames[i], amt);
    }
    Arrays.sort(horses, Comparator.reverseOrder());
    System.out.println("The current race progress is :");
    for (int i = 0; i < horses.length; i++) {
        System.out.println(horses[i]);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140149&siteId=1