views:

103

answers:

2

Right, so why does Java come up with this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from double to int

at rainfall.main(rainfall.java:38)

From this:

public class rainfall {

 /**
  * @param args
  */
 public static void main(String[] args) 
 {
 int[]  numgroup;
 numgroup = new int [12];
 ConsoleReader console = new ConsoleReader();
 int highest;
 int lowest;
 int index;
 int tempVal;
 int minMonth;
    int minIndex;
 int maxMonth;
 int maxIndex;


 System.out.println("Welcome to Rainfall");
 // Input (index now 0-based)
 for(index = 0; index < 12; index = index + 1)
 {       
     System.out.println("Please enter the rainfall for month " + index + 1);
     tempVal = console.readInt();
     while (tempVal>100 || tempVal<0)
     {
         System.out.println("The rating must be within 0...100. Try again");
         tempVal = console.readInt();
     }
     numgroup[index] = tempVal;
 }           

 lowest = numgroup[0];
 highest = numgroup[0];
 int total = 0.0;
 // Loop over data (using 1 loop)
 for(index = 0; index < 12; index = index + 1)
 {       
     int curr = numgroup[index];
     if (curr < lowest) {
         lowest = curr;
         minIndex = index;
     }
     if (curr > highest) {
         highest = curr;
         maxIndex = index;
     }
      total += curr;
 }
 float avg = (float)total / numgroup.length;

 System.out.println("The average monthly rainfall was " + avg);
 // +1 to go from 0-based index to 1-based month
 System.out.println("The lowest monthly rainfall was month " + minIndex + 1);
 System.out.println("The highest monthly rainfall was month " + maxIndex + 1);

 System.out.println("Thank you for using Rainfall");

 }


 private static ConsoleReader ConsoleReader() {

  return null;
 }

}
+3  A: 

I guess the culprit is this line:

int total = 0.0;

should be

int total = 0;

instead.

Péter Török
That then throws an error here: System.out.println("The lowest monthly rainfall was month " + minIndex + 1); System.out.println("The highest monthly rainfall was month " + maxIndex + 1);
Emily
If its a compilation error, try using brackets, like `System.out.println("The lowest monthly rainfall was month " + (minIndex + 1));`. Please be more specific about errors in general.
Péter Török
Nope, still comes up with this error: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The local variable minIndex may not have been initialized The local variable maxIndex may not have been initialized
Emily
Initialize them at the declaration, like `int minIndex = 0;`. Without this, there may be execution paths where these variables are not initialized. Namely, if all rainfall values are equal. Since you initialize `lowest` and `highest` to the rainfall value of the first month, you must be consistent to initialize `minIndex` and `maxIndex` to refer to the first month as well.
Péter Török
+1  A: 

Problem is with this line here: int total = 0.0;

Need to change total to be of type float

David Relihan
I think you mean `double`. I'm not sure why you'd want to use `float`, but if you did, you'd need to change `0.0` to `0.0f` for it to work.
Syntactic
Just to be consistant with rest of code in example
David Relihan