2 Replies - 10433 Views - Last Post: 19 August 2008 - 08:29 AM Rate Topic: -----

#1 summer291   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 07

java.lang.ArrayIndexOutOfBoundsException: 13

Post icon  Posted 19 August 2008 - 05:51 AM

Hi I had to create a program that has an array of double of size 12 to store the average monthly temperatures for one year.
I Had to findThe hottest and coldest months of the year.
The average temperature for the year
The difference between the hottest and coldest months of the year.
The average temperature of any given month. The month is specified by input values: month (1,…,12) . Reject invalid input values (less than 1 and greater than 12 for month).


Things seem to be better but now I'm getting all my exception message and also java.lang.ArrayIndexOutOfBoundsException: 13 error messahe



Thanks


class InvalidMonthException extends Exception {
		 public InvalidMonthException(String message) {
			super(message);
		 }
}

//Object that holds the temperatures and the methods
class Temperatures {
//The array to hold the temperatures
	   private double temperatures[];

	   public Temperatures() {
//Create an array of doubles to hold the temperatures
			   temperatures = new double[12];
	   }

	   public Temperatures( double temps[] ) {
			   // create an array of doubles to hold the temperatures, initially set to the values given
			   temperatures = new double[12];
			   for (int i = 0; i < 12; i++) {
					   temperatures[i] = temps[i];
			   }
	   }

// Method to return the hottest month of the year
	   public double getHottest() {
			   // find the largest entry
			   double theMax = temperatures[0];
			   for (int i = 0; i < 12; i++) {
					   // if we've found a larger temperature, then keep track of it
					   if ( temperatures[i] <= theMax ) {
							   theMax = temperatures[i];
					   }
			   }
			   return theMax;
	   }

// Method to return the coldest month of the year
	   public double getColdest() {
			   // find the smallest entry
			   double theMin = temperatures[0];
			   for (int i = 0; i < 12; i++) {
					   // if we've found a smaller temperature, then keep track of it
					   if ( temperatures[i] <= theMin ) {
							   theMin = temperatures[i];
					   }
			   }
			   return theMin;

	   }

//Method to return the average temperature for the year
	   public double getAvgYearTemp() {
			   // sum up all the entries in the array
			   double total = 0;
			   for (int i = 0; i < 12; i++) {
					   total += temperatures[i];
			   }

			   // compute the average and return it
			   return total / 12.0;
	   }

//Method to return the difference between the hottest and the coldest months
	   public double getHotColdDiff() {
			   return getHottest() - getColdest();
	   }

// Method to return the average temp for the specified month, month is 1...12
		double getMonthAvgTemp(int month) throws InvalidMonthException {
			   if ( (1 <= month) && (month <= 12) ) {
					   return temperatures[month-1];
			   } else {
					   throw new InvalidMonthException( month + "is an invalid month. Need to be between 1 and 12.");
			   }
	   }

	   // set the average temp for the specified month, month is 1...12
	   public void setMonthAvgTemp(int month, double t) throws InvalidMonthException {
			   if ( (1 >= month) && (month <= 12) ) {
					   temperatures[month-1] = t;
			   } else {
					   throw new InvalidMonthException( month + "is an invalid month. Need to be between 1 and 12.");
			   }
	   }
}


public class Program2 {

	   public static void main(String args[]) {

			   // setup the temperature object with some values
			   double initialTemps[] = {10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,80.0,70.0,60.0};
			   Temperatures t = new Temperatures(initialTemps);

			   // print out the temperatures, try two invalid months (0 and 13)
			   for (int i = 0; i <= 13; i++) {
					   try {
							   System.out.println("Month" + i  + t.getMonthAvgTemp(i) );
					   } catch (Exception e) {
							   System.out.println(e);
					   }
			   }

			   // call the methods to summarize the data
			   System.out.println("Hottest month" + t.getHottest() );
			   System.out.println("Coldest month"  + t.getColdest() );
			   System.out.println("Difference between hot and cold " + t.getHotColdDiff() );
			   System.out.println("Average Temp." + t.getAvgYearTemp() );
	   }
}

This post has been edited by summer291: 19 August 2008 - 11:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.ArrayIndexOutOfBoundsException: 13

#2 oldSwede   User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 464
  • Joined: 08-January 16

Re: java.lang.ArrayIndexOutOfBoundsException: 13

Posted 19 August 2008 - 07:02 AM

I think
temperatures[i] <= theMax

ought to be
temperatures[i] >= theMax


Since you say that your calculations (not your coding) is all messed up I just took a quick look at your code. I'm not very good at Java but I think you should be okay now after the above corrections of max and min.

What problem is still left after this minor correction? Try to be precise about what happens when, and what should have happened then.

Regards
/Jens

This post has been edited by jens: 19 August 2008 - 07:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 lordms12   User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: java.lang.ArrayIndexOutOfBoundsException: 13

Posted 19 August 2008 - 08:29 AM

Putting each class in its own file is the default in java unless it is an inner class.
I think this is what you want (I did not write all the months please complete them in String[] MONTHS).
public class Program2 {
	public static final String[] MONTHS = {"", "Jan", "Feb", "Mar", "Apr", "", "", "", "", "", "", "", "", "", "", "", ""};//complete this

	public static void main(String args[]) {
		// setup the temperature object with some values
		double initialTemps[] = {10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,80.0,70.0,60.0};
		Temperatures t = new Temperatures(initialTemps);


		// print out the temperatures, try two invalid months (0 and 13)
		for (int i = 0; i <= 13; i++) {
			try {
				System.out.println(MONTHS[i] + " " + t.getMonthAvgTemp(i) );
			} catch (Exception e) {
				System.out.println(e);
			}
		}


		try {
			// call the methods to summarize the data
			int hottest = t.getHottest();
			System.out.println("Hottest " + MONTHS[hottest] + t.getMonthAvgTemp(hottest) );//t.getHottest()
		} catch (Exception e) {
			System.out.println(e);
		}
		try {
			int coldest = t.getColdest();
			System.out.println("Coldest " + MONTHS[coldest] + t.getMonthAvgTemp(coldest) );
		} catch (Exception e) {
			System.out.println(e);
		}
		try {
			System.out.println("Difference between hot and cold " + t.getHotColdDiff() );
		} catch (Exception e) {
			System.out.println(e);
		}
		System.out.println("Average Temp." + t.getAvgYearTemp() );

	}
}

class InvalidMonthException extends Exception {
	public InvalidMonthException(String message) {
		super(message);
	}
}

//Object that holds the temperatures and the methods
class Temperatures {
	//The array to hold the temperatures
	private double temperatures[];

	public Temperatures() {
		//Create an array of doubles to hold the temperatures
		temperatures = new double[12];
	}

	public Temperatures( double temps[] ) {
		// create an array of doubles to hold the temperatures, initially set to the values given
		temperatures = new double[12];
		for (int i = 0; i < 12; i++) {
			temperatures[i] = temps[i];
		}
	}

	// Method to return the hottest month of the year
	public int getHottest() {
		// find the largest entry
		double theMax = temperatures[0];
		int tempI = 0;
		for (int i = 0; i < 12; i++) {
			// if we've found a larger temperature, then keep track of it
			if ( temperatures[i] >= theMax ) {
				theMax = temperatures[i];
				tempI = i;
			}
		}
		return tempI;
	}

	// Method to return the coldest month of the year
	public int getColdest() {
		// find the smallest entry
		double theMin = temperatures[0];
		int tempI = 0;
		for (int i = 0; i < 12; i++) {
			// if we've found a smaller temperature, then keep track of it
			if ( temperatures[i] <= theMin ) {
				theMin = temperatures[i];
				tempI = i;
			}
		}
		return tempI;

	}

	//Method to return the average temperature for the year
	public double getAvgYearTemp() {
		// sum up all the entries in the array
		double total = 0;
		for (int i = 0; i < 12; i++) {
			total += temperatures[i];
		}

		// compute the average and return it
		return total / 12.0;
	}

	//Method to return the difference between the hottest and the coldest months
	public double getHotColdDiff() throws InvalidMonthException {
		return getMonthAvgTemp(getHottest()) - getMonthAvgTemp(getColdest());
	}

	// Method to return the average temp for the specified month, month is 1...12
	double getMonthAvgTemp(int month) throws InvalidMonthException {
		if ( (1 <= month) && (month <= 12) ) {
			return temperatures[month-1];
		} else {
			throw new InvalidMonthException( month + " is an invalid month. Need to be between 1 and 12.\n");
		}
	}

	// set the average temp for the specified month, month is 1...12
	public void setMonthAvgTemp(int month, double t) throws InvalidMonthException {
		if ( (1 >= month) && (month <= 12) ) {
			temperatures[month-1] = t;
		} else {
			throw new InvalidMonthException( month + " is an invalid month. Need to be between 1 and 12.\n");
		}
	}
}

This post has been edited by lordms12: 19 August 2008 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1