Java - Highest, Lowest and Average

Posted by Emily on Stack Overflow See other posts from Stack Overflow or by Emily
Published on 2010-05-17T09:45:55Z Indexed on 2010/05/17 9:50 UTC
Read the original article Hit count: 351

Filed under:
|
|
|

Hello,

I've just started studying and I need help on one of my exercises.

I need the end user to input a rain fall number for each month. I then need to out put the average rainfall, highest month and lowest month and the months which rainfall was above average.

I keep getting the same number in the highest and lowest and I have no idea why. I am seriously pulling my hair out. Any help would be greatly appreciated.

This is what I have so far:

public class rainfall {

/**
 * @param args
 */
public static void main(String[] args) 
{
int[]  numgroup;
numgroup = new int [13];
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");

for(index = 1; index < 13; index = index + 1)
{       
    System.out.println("Please enter the rainfall for month " + index);
            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];


    for(minIndex = 0; minIndex < numgroup.length; minIndex = minIndex + 1);
    {
            if (numgroup[0] < lowest)
            {
            lowest = numgroup[0];
            minMonth = minIndex;
            }
    }

highest = numgroup[1];


        for(maxIndex = 0; maxIndex < numgroup.length; maxIndex = maxIndex + 1);
        {
                if (numgroup[1] > highest)
                {
                highest = numgroup[1];
                maxMonth = maxIndex;
                }
        }


    System.out.println("The average monthly rainfall was ");
    System.out.println("The lowest monthly rainfall was month " + minIndex);
    System.out.println("The highest monthly rainfall was month " + maxIndex);

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

}


private static ConsoleReader ConsoleReader() {

    return null;
}

}

Thanks,

Emily

© Stack Overflow or respective owner

Java - Highest, Lowest and Average

Posted by Emily on Stack Overflow See other posts from Stack Overflow or by Emily
Published on 2010-05-17T10:27:38Z Indexed on 2010/05/17 10:30 UTC
Read the original article Hit count: 351

Filed under:
|
|
|

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; }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays