0

I'm stuck... My code was working earlier, but now it just hangs. On top of that I can't seem to get my getHighest and getSmallest methods to return the correct values. I don't know if I'm just not catching something. Any help would be great!

import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
 public static void main(String[] args)
 {
 final int MONTHS = 12;
 double[] rain = new double[MONTHS];
 initRain(rain);
 double total = totalRain(rain);
 double average = averageRain(rain, total);
 double most = mostRain(rain);
 double least = leastRain(rain);
 DecimalFormat digit = new DecimalFormat("#.0");
 System.out.println("The total rainfall of the year is " + digit.format(total));
 System.out.println("The average rainfall of the year is " + digit.format(average));
 System.out.println("The month with the highest amount of rain is " + most);
 System.out.println("The month with the lowest amount of rain is " + least);
 }
 public static void initRain(double[] array)
 {
 Scanner keyboard = new Scanner(System.in);
 for (int x = 0; x < array.length; x++)
 { 
 System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
 array[x] = keyboard.nextDouble();
 }
 }
 public static double totalRain(double[] array)
 {
 double total = 0;
 for (int x = 0; x < 12; x++)
 total += array[x];
 return total;
 }
 public static double averageRain(double[] array, double total)
 {
 return total / array.length;
 }
 public static double mostRain(double[] array)
 {;
 double maximum = array[1];
 int value = 0;
 for (int i=0; i < 12; i = i++)
 {
 if (array[i] >= maximum)
 maximum = array[i];
 value = i;
 }
 return value;
 }
 public static double leastRain(double[] array)
 {
 double minimum = array[0];
 int value;
 for (int i=0; i < 12; i++)
 {
 if (array[i] <= minimum) 
 minimum = array[i];
 value = i;
 }
 return value;
 }
}
stealthyninja
10.4k11 gold badges55 silver badges61 bronze badges
asked Dec 6, 2012 at 16:34
10
  • 1
    In the leastRain() and mostRain() methods you are returning value in the for loop, but you should actually return it after the loop... Commented Dec 6, 2012 at 16:38
  • Also, when iterating over arrays, it is pretty much never ever a good idea to hard code the length into a for loop... even if you dont think it will ever change. Always use array.length, or at least a variable that you can quickly change later if the array length changes. for (int i=0; i < array.length ; i++) Commented Dec 6, 2012 at 16:40
  • Ahhh thank you! that did help, can you explain why code is just hangin right now though :( I greatly appreciate it! Commented Dec 6, 2012 at 16:41
  • You've also got an interesting i = i++ in the mostRain(...) for loop. That's your problem. You are setting i to the value of i before it is incremented. So in effect not incrementing i. Commented Dec 6, 2012 at 16:41
  • I am not really sure what you mean by hanging. You mean it is running, but not producing any discernible result? Is it throwing an exception? Commented Dec 6, 2012 at 16:42

1 Answer 1

3

Your program hangs because of this line:

for (int i=0; i < 12; i = i++)

The problem is that i++ returns the value of i before the variable is incremented, so the increment step of your loop is the same as writing i=i. Thus, the variable i never reaches the escape condition of the loop.

it should be:

for (int i=0; i < 12; i++)

Cleaned up your code a little bit, can be improved a lot. There were many simple errors.

import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
 public static void main(String[] args)
 {
 final int MONTHS = 12;
 double[] rain = new double[MONTHS];
 initRain(rain);
 double total = totalRain(rain);
 double average = averageRain(rain, total);
 int most = mostRain(rain);
 int least = leastRain(rain);
 DecimalFormat digit = new DecimalFormat("#.0");
 System.out.println("The total rainfall of the year is " + digit.format(total));
 System.out.println("The average rainfall of the year is " + digit.format(average));
 System.out.println("The month with the highest amount of rain is " + (most + 1));
 System.out.println("The month with the lowest amount of rain is " + (least + 1));
 }
 public static void initRain(double[] array)
 {
 Scanner keyboard = new Scanner(System.in);
 for (int x = 0; x < array.length; x++)
 {
 System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
 array[x] = keyboard.nextDouble();
 }
 }
 public static double totalRain(double[] array)
 {
 double total = 0;
 for (int x = 0; x < 12; x++)
 total += array[x];
 return total;
 }
 public static double averageRain(double[] array, double total)
 {
 return total / array.length;
 }
 public static int mostRain(double[] array)
 {
 double maximum = array[1];
 int value = 0;
 for (int i=0; i < 12; i++) {
 if (array[i] >= maximum) {
 maximum = array[i];
 value = i;
 }
 }
 return value;
 }
 public static int leastRain(double[] array)
 {
 double minimum = array[0];
 int value = 0;
 for (int i=0; i < 12; i++)
 {
 if (array[i] <= minimum) {
 minimum = array[i];
 value = i;
 }
 }
 return value;
 }
}
le3th4x0rbot
2,46926 silver badges35 bronze badges
answered Dec 6, 2012 at 16:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.