views:

269

answers:

7

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

+1  A: 

instead of

if (numgroup[0] < lowest)

you have to write

if (numgroup[minIndex] < lowest)

the same goes for

if (numgroup[1] > highest)

which should be

if (numgroup[maxIndex] > highest)
ammoQ
Thanks, so I swapped these out, but I keep getting this error:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13 at rainfall.main(rainfall.java:43)
Emily
+1  A: 

You do

lowest = numgroup[0]

and then

if (numgroup[0] < lowest)

which will never be "true" as numgroup[0] is always equal to lowest. Rather your if-clause should be something like if (numgroup[minIndex] < lowest). The same thing applies for highest.

Select0r
A: 
public static void main(String[] args) 
{
int[] numgroup = new int [12]; // 12 months - 12 elements
ConsoleReader console = new ConsoleReader();
int highest;
int lowest;
int index;
int tempVal;
int minIndex;
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 " + agv);
// +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");

}
extraneon
So I tried this and its coming up with: 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 at rainfall.main(rainfall.java:59)
Emily
A: 

Just try to use collections for min/max if there is no restriction in your homework.

Vector<Integer> rainfallData = new Vector<Integer>();
int avg = 0;

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();
    }
   rainfallData.add(tempVal);
   avg+=tempVal; 
}

avg /= rainfallData.size();
int min = Collections.min(rainfallData);
int max = Collections.max(rainfallData);

Otherwise a min/max should look like this:

public int min(int[] vals) {
    if( vals==null || vals.length==0} {
           throw new IllegalArgumentException();
    } else if( vals.length == 1 ) {
         return vals[0];
    }
    int min = vals[0]; // Dont initialize with Integer.MAX_VALUE or so
    for(int i = 1; i < vals.length; ++i ) {
        if( vals[i] < min ) {
           min = vals[i];
        }
    } 
    return min;
}
InsertNickHere
A: 
for (index = 0; index < 12; index++) {

}

change the first for loop and the following

lowest = numgroup[0];

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

highest = numgroup[0];

for (maxIndex = 0; maxIndex < numgroup.length; maxIndex = maxIndex + 1)
{
    if (numgroup[maxIndex] > highest) {
         highest = numgroup[maxIndex];
     }
}
TuxGeek
Nope, this doesn't work, it outputs:The lowest monthly rainfall was month 12The highest monthly rainfall was month 12
Emily
+3  A: 

First, since this is your homework, you should not be asking it on stackoverflow.com

Now let have a look at your code

  1. lowest = numgroup[0];

Why? It seems that you trying to use this algorithm to find min:

1.1 Suppose first number (which you think is numgroup[0]) is min (named as lowest in your code)
1.2. Compare it with all other numbers, if any of the numbers is smaller, replace min (i.e lowest).

But, numgroup[0] is not your first number! You started your first for loop like this

for(index = 1;...

So, your first number is numgroup[1].

Next, your second loop starts like

for(minIndex = 0;

whereas the element at index 0 is never even intended to be used by you (I guess)

Next, your condition for finding out if the number in current iteration is less than lowest is

if (numgroup[0] < lowest)

which always compares element at index 0 with lowest which (I guess) is not your intention.

Omer Akhter
This is what I said, but since this is better formatted, +1 to this and I will delete my answer.
Moron
A: 

Look, I'm so confused. I've had two online lectures on Java, which obviously weren't the best and I need to hand this in. If anyone can tell me what I'm doing wrong, preferably by responding with an answer with // comments about why my code sucks, I would be over the moon. Its 9pm here and I just want to figure this out.

I keep getting this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 at rainfall.main(rainfall.java:43)

This is where I'm at, FYI, I start at index=1 so that when it prints it comes up as month 1 not month 0:

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 = 0; index < 12; index = index + index)
{       
    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[minIndex] < lowest)
            {
            lowest = numgroup[minIndex];
            }
    }

highest = numgroup[0];

        for(maxIndex = 0; maxIndex < numgroup.length; maxIndex = maxIndex + 1);
        {
                if (numgroup[maxIndex] > highest)
                {
                highest = numgroup[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;
}

}

Emily
@Emily: you have index+index instead of index+1. Also asking questions as part of an answer is against the rules of the site.
Moron