3 Replies - 867 Views - Last Post: 21 November 2011 - 03:59 PM Rate Topic: -----

#1 Cambron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-October 11

Average rainfall

Posted 21 November 2011 - 03:41 PM

Hello again!
I am having difficulty finding out where I'm making the mistake. Do not need anyone to "fix" it for me, but maybe point me in the right direction as to where it is lol.
import java.util.Scanner;
import java.text.DecimalFormat;

class RainFall
{
	private double rain[];
	
	public RainFall(double s[])
	{
		rain = new double[s.length];
		for(int index = 0; index < s.length; index++)
			rain[index] = s[index];
	}
	
	public double getTotal()
	{
		double total = 0.0;
		for(int index = 0; index < rain.length; index++)
			total += rain[index];
		return total;
	}
	
	public double getAverage()
	{
		return getTotal() / rain.length;
	}
	
	public double getHighest()
	{
		double highest = rain[0];
		for(int index = 1; index < rain.length; index++)
		{
			if (rain[index] > highest)
				highest = rain[index];
		}
	return highest;
	}
	
	public double getLowest()
	{
		int index = 0;
		double lowest = rain[0];
		for(index = 1; index < rain.length; index++);
		{
			if(rain[index] < lowest)
				lowest = rain[index];
		}
		return lowest;
	}
	
}

class RainfallDemo
{
	public static void main(String[] args)
	{
		int One_Week = 12;
		double rain[] = new double[One_Week];
		getValues(rain);
		RainFall year = new RainFall(rain);
		DecimalFormat dollar = new DecimalFormat("0.00");
		System.out.println("The total rainfall is: " + dollar.format(year.getTotal()) +
						   "\nThe average rainfall is: " + dollar.format(year.getAverage()) +
						   "\nThe highest rainfall is: " + dollar.format(year.getHighest()) +
						   "\nThe lowest rainfall is: " + dollar.format(year.getLowest()));
						   
		System.exit(0);
		
	}
	
private static void getValues(double array[])
{
	String input;
	Scanner keyboard = new Scanner(System.in);
	
	for(int i = 0; i < array.length; i++)
	{
		System.out.print("Enter the rainfall for the month " + (i + 1) + ":");
		array[i] = keyboard.nextDouble();
		if(array[i] < 0)
		{
			System.out.print("Invalid entry. Renter rainfall for the month " + (i + 1) + ":");
			array[i] = keyboard.nextDouble();
		}
	}
}
}


error is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at RainFall.getLowest(RainFall.java:45)
at RainfallDemo.main(RainFall.java:62)
Thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: Average rainfall

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Average rainfall

Posted 21 November 2011 - 03:42 PM

And you posted Java in C and C++ why, exactly?

Moved to Java.
Was This Post Helpful? 0
  • +
  • -

#3 Cambron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-October 11

Re: Average rainfall

Posted 21 November 2011 - 03:53 PM

View PostJackOfAllTrades, on 21 November 2011 - 03:42 PM, said:

And you posted Java in C and C++ why, exactly?

Moved to Java.

GAAAH. sorry about that. Prob because I have yet to ask a java question here. Been C up until now.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Average rainfall

Posted 21 November 2011 - 03:59 PM

Some comments in your code:
- First you pass an array from main() to getValues() and do changes there expecting changes will affect the array in main()!

- This constructor is just complication:
public RainFall(double s[])
	{
		rain = new double[s.length];
		for(int index = 0; index < s.length; index++)
			rain[index] = s[index];
	}

You ca just say:
 rain = s;
and will take everything from it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1