0

So i have a program that gets measurements of rain from the user and stores it into an array with the help of a Class RainFall that just has basic set/get mutators and accessors. My problem is that for some reason the program crashes on the Arrays.sort() line. Is it that i need to create a new method just for that line? If so what would that look like?

import java.util.Arrays;
import java.util.Scanner;
public class Lab7rain {

  public static void main(String[] args) {
    double amount=0;//VARIABLE DECLARATIONS
    RainFall r = new RainFall(amount);//CREATE OBJECT
    Scanner kb = new Scanner(System.in);//ALLOCATE SPACE
    RainFall[] month = new RainFall[12];

    for(int i=0; i<12; i++){
     System.out.println("What was the amount of rain in "+r.date()+"?");//rain per month
     amount = kb.nextDouble();//gets input
     month[i]= new RainFall(amount); //stores amount into array slot
     System.out.println();
     r.setTotal(amount);//sends amount to method that adds them together to create a total
    } 

    kb.close();

    r.setAvg();//initiates method that divides total by 12 to get average rain fall per month

    System.out.println(r.getTotal());//print total rain fall
    System.out.println(r.getAvg());//print average rain fall
    System.out.println();//skip line

    Arrays.sort(month);//i want this to sort the array so i can
    //then print out the highest and lowest months
    System.out.println(month[0]+" "+month[11]);
  }
}

Exception in thread "main" java.lang.ClassCastException: lab7rain.RainFall cannot be cast to java.lang.Comparable at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290) at java.util.ComparableTimSort.sort(ComparableTimSort.java:157) at java.util.ComparableTimSort.sort(ComparableTimSort.java:146) at java.util.Arrays.sort(Arrays.java:472) at lab7rain.Lab7rain.main(Lab7rain.java:46) Java Result: 1

  • 4
    You probably need to implement `Comparable` interface in the `RainFall` object – nem035 Oct 12 '14 at 02:05
  • This is the error message Exception in thread "main" java.lang.ClassCastException: lab7rain.RainFall cannot be cast to java.lang.Comparable at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290) at java.util.ComparableTimSort.sort(ComparableTimSort.java:157) at java.util.ComparableTimSort.sort(ComparableTimSort.java:146) at java.util.Arrays.sort(Arrays.java:472) at lab7rain.Lab7rain.main(Lab7rain.java:46) Java Result: 1 – learningCurve Oct 12 '14 at 02:19
  • 1
    Then @nem has solved your problem -- make sure that RainFall implements `Comparable` complete with a sensible `public int compareTo(RainFall o)` method. Either that or use a Comparator. – Hovercraft Full Of Eels Oct 12 '14 at 02:22

2 Answers2

3

Based on your error in the comment section, you need to implement the Comparable interface on the RainFall class before you can use the Arrays.sort() method or use a Comparator as @ElliotFrisch showed you in his answer.

public class RainFall implements Comparable<RainFall> {

    double amount;
    // your code

    public int compareTo(RainFall r) {
        return Double.compare(this.amount, r.amount); 
    }

}
nem035
  • 34,790
  • 6
  • 87
  • 99
  • When i use this method i get a strange print out. Where i say System.out.println(month[0]); or when i say (month[0].toString); It gives me something like @33tjh43. Any ideas? – learningCurve Oct 14 '14 at 21:24
  • 1
    You need to override the `toString()` method for the `RainFall` class. http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – nem035 Oct 14 '14 at 21:41
2

Your class RainFall needs to implement Comparable<RainFall>, or you could write a Comparator<RainFall> and use the Arrays.sort() that takes that. For example,

static class RainFallComparator implements Comparator<RainFall> {
  public int compare(RainFall a, RainFall b) {
    if (a.getAmount() < b.getAmount()) return -1;
    else if (a.getAmount() > b.getAmount()) return 1;
    return 0;
  }
}

and then

 Arrays.sort(month, new RainFallComparator());
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249