Question

Arrays.sort() gives and error of:

FiveDice.java:19: error: no suitable method found for sort(int)
   Arrays.sort(compNums);

If I take anything out of the for loop, it thinks thee is only 1 number or gives an error. What other sorting options would be usable?

import java.util.*;
public class FiveDice {
    public static void main(String[] args) {
        int x;
        int compNums = 0;
        int playerNums;

        Die[] comp = new Die[5];
        Die[] player = new Die[5];
        System.out.print("The highest combination wins! \n5 of a kind, 4 of a kind, 3 of a kind, or a pair\n");
//computer
        System.out.print("Computer rolled:  ");
        for(x = 0; x < comp.length; ++x) {
            comp[x] = new Die();
            compNums = comp[x].getRoll();
           //Arrays.sort(compNums); <--does not work
           System.out.print(compNums + " ");
        }
//player
        System.out.print("\nYou rolled: \t  ");
        for(x = 0; x < player.length; ++x) {
            player[x] = new Die();
            playerNums = player[x].getRoll();
            System.out.print(playerNums + " ");
        }
    }
}

die class

public class Die {
    int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;
    public Die()
       {   } 
    public int getRoll() { 
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
        return roll; }
    public void setRoll()
        { this.roll = roll; } 

}

Was it helpful?

Solution 2

You don't have to use 2 arrays for the sorting. If you implement the Comparable<T> interface, your classes can be sorted by Java Collections API. In your case, Die class can implement Comparable<T> and provide a way for the Java framework to compare dice values.

Take a look at the Java API: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

In your case:

public class Die implements Comparable<Die>{

    int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;

    public Die() {   } 

    public int computeRoll() { 
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
        return roll;
    }

    // I also changed this to store the value
    public int getRoll() { 
        return roll; 
    }
    public void setRoll() { this.roll = roll; }

    // This is the method you have to implement
    public int compareTo(Die d) {
        if(getRoll() < d.getRoll()) return -1;
        if(getRoll() > d.getRoll()) return +1;
        if(getRoll() == d.getRoll()) return 0;
    }

}

Whenever you have a Collection of Dies, like an ArrayList or LinkedList, you simply sort the collection itself. Below is a sample code.

ArrayList<Die> myCollection = new ArrayList<Die>();
myCollection.add(die1);
// more array population code
// ...
Collections.sort(myCollection);

OTHER TIPS

Easiest way is to implement Comparable to Die class , set value of roll in constructor of Die not in the getter method and your problem is solved.

public class Die implements Comparable<Die> {
    private int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;
    public Die() {
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
    }


    public int getRoll() {
         return roll;
    }        
    public void setRoll(int roll) {
         this.roll = roll;
    }

    public int compareTo(Die d) {
         return new Integer(d.getRoll()).compareTo(new Integer(this.getRoll()));
    }

}

now Arrays.sort(Die[]) will sort the array of Die.

You can't perform sort() on compNums because it is a single value. You declare it as an int value, rather than as an array of integers.

Instead, you should make compNums an array, populate it with the Die roll values, and then perform a sort operation on the resultant array. I believe the following will achieve what you are after.

int[] compNums = new int[5];
...
for(x = 0; x < comp.length; ++x) {
    comp[x] = new Die();
    compNums[x] = comp[x].getRoll();
}
Arrays.sort(compNums);
// print results

you must pass an array to the Array.Sort(arr) not parameter you must do something like this Array.Sort(int[]compNums);

and if you are using a anonymous type like comp[]compNums you must do it like this

java.util.Arrays.sort(comp[]compNums, new java.util.Comparator<Object[]>() {
        public int compare(Object a[], Object b[]) {
            if(something)
                return 1;

                return 0;
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top