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