Hi Rama,
The ArrayIndexOutOfBoun
In your program, as soon as the value of j hits 9, the expression "temperature[ i][j+1]" tries to access the 11th element - which does not exist and hence throws an exception.
The solution is to change the line
for (int j=0; j<10; j++)
to
for (int j=0; j<9; j++)
Hope this helps.
And happy learning!
Regards,
Trishul
P.S. Contrary to many experts, I suggest you to use Eclipse and use the Debugger so that you can visually see whats happening to your program.
____________
From: Sri Rama Krishnamurthi <srmurthi1@yahoo.
To: Java_Official@
Sent: Sat, December 5, 2009 9:40:44 PM
Subject: [Java] Exception in thread "main" java.lang.ArrayInde
Hi,
I've started learning Java and creating small programs and compiling on my own using EditPlus Editor and Command Prompt.
No doubt, my prorgram will tell you I'm a beginner.
javac Temperature. java was successful.
When I fire java Temperature I got the follwoing error.
Exception in thread "main" java.lang.ArrayInde xOutOfBoundsExce ption: 10
at Temperature. main(Temperature .java:44)
I can see my "if clause" compares the values in the first row from the Print staements. After that it's throwing error. Somebody please help me to find out and fix this error.
Thanks in advance
Sri
Program Starts
class Temperature
//To get the RowNum, ColumnNum of the highest and lowest teperature from the array temperature
{
public static void main(String[ ] args)
{
double temp;
int maxDay = 0, maxCityCode = 0, minDay, minCityCode;
double temperature [ ][ ] = {
{ 25.00, 24.50, 23.00, 23.50, 24.00, 24.50, 25.00, 25.50, 26.00, 26.50 },
{ 23.00, 24.00, 25.00, 26.00, 27.00, 28.00, 29.00, 30.00, 31.00, 32.00 },
{ 21.00, 23.00, 24.00, 23.50, 25.00, 25.00, 23.00, 26.00, 27.00, 30.00 },
{ 24.00, 23.67, 25.00, 24.33, 26.33, 26.33, 23.67, 27.67, 29.00, 33.00 },
{ 23.50, 23.92, 25.50, 24.33, 26.83, 26.58, 22.67, 27.92, 29.50, 34.75 },
{ 23.90, 24.17, 26.00, 24.33, 27.33, 26.83, 21.67, 28.17, 30.00, 36.50 },
{ 24.30, 24.42, 26.50, 24.33, 27.83, 27.08, 20.67, 28.42, 30.50, 38.25 },
{ 24.70, 24.67, 27.00, 24.33, 28.33, 27.33, 19.67, 28.67, 31.00, 40.00 },
{ 25.10, 24.92, 27.50, 24.33, 28.83, 27.58, 18.67, 28.92, 31.50, 41.75 },
{ 25.50, 25.17, 28.00, 24.33, 29.33, 27.83, 17.67, 29.17, 32.00, 43.50 },
{ 25.90, 25.42, 28.50, 24.33, 29.83, 28.08, 16.67, 29.42, 32.50, 45.25 },
{ 26.30, 25.67, 29.00, 24.33, 30.33, 28.33, 15.67, 29.67, 33.00, 47.00 },
{ 26.70, 25.92, 29.50, 24.33, 30.83, 28.58, 14.67, 29.92, 33.50, 48.75 },
{ 27.10, 26.17, 30.00, 24.33, 31.33, 28.83, 13.67, 30.17, 34.00, 50.50 },
{ 27.50, 26.42, 30.50, 24.33, 31.83, 29.08, 12.67, 30.42, 34.50, 52.25 },
{ 27.90, 26.67, 31.00, 100.00, 32.33, 29.33, 11.67, 30.67, 35.00, 54.00 },
{ 28.30, 26.92, 31.50, 24.33, 32.83, 29.58, 10.67, 30.92, 35.50, 55.75 },
{ 28.70, 27.17, 32.00, 24.33, 33.33, 29.83, 9.67, 31.17, 36.00, 57.50 },
{ 29.10, 27.42, 32.50, 24.33, 33.83, 30.08, 8.67, 31.42, 36.50, 59.25 },
{ 29.50, 27.67, 33.00, 24.33, 34.33, 30.33, 7.67, 31.67, 37.00, 61.00 },
{ 29.90, 27.92, 33.50, 24.33, 34.83, 30.58, 6.67, 31.92, 37.50, 62.75 },
{ 30.30, 28.17, 34.00, 24.33, 35.33, 30.83, 5.67, 32.17, 38.00, 64.50 },
{ 30.70, 28.42, 34.50, 24.33, 35.83, 31.08, 4.67, 32.42, 38.50, 66.25 },
{ 31.10, 28.67, 35.00, 24.33, 36.33, 31.33, 3.67, 32.67, 39.00, 68.00 },
{ 31.50, 28.92, 35.50, 24.33, 36.83, 31.58, 2.67, 32.92, 39.50, 69.75 },
{ 31.90, 29.17, 36.00, 24.33, 37.33, 31.83, 1.67, 33.17, 40.00, 71.50 },
{ 32.30, 29.42, 36.50, 24.33, 37.83, 32.08, 0.67, 33.42, 40.50, 73.25 },
{ 32.70, 29.67, 37.00, 24.33, 38.33, 32.33, -0.33, 33.67, 41.00, 75.00 },
{ 33.10, 29.92, 26.73, 24.33, 38.83, 32.58, -1.33, 33.92, 41.50, 76.75 },
{ 33.50, 30.17, 26.83, 24.33, 39.33, 32.83, -2.33, 34.17, 42.00, 78.50 },
{ 33.90, 30.42, 26.93, 24.33, 39.83, 33.08, -3.33, 34.42, 42.50, 80.25 }
};
for (int i=0; i<31; i++)
{
for (int j=0; j<10; j++)
{
System.out.println( "temp =" + temperature[ i][j] + " , " + temperature[ i][j+1]);
if (temperature[ i][j] < temperature[ i][j+1])
{
temp = temperature[ i][j];
temperature[ i][j] = temperature[ i][j+1];
temperature[ i][j+1] = temp;
maxDay = i+1;
maxCityCode = j+1;
}
else
{
minDay = 1;
minCityCode = j+2;
}
System.out.println( "Next Value");
}
}
System.out.println( "Maximum Temperature on Day" + maxDay);
System.out.println( "Maximum Temperature on City" + maxCityCode) ;
}
}
Program Ends
Output in command Prompt
F:\UDX\Sample_ Java_Programs> javac Temperature. java
F:\UDX\Sample_ Java_Programs> java Temperature
temp =25.0 , 24.5
Next Value
temp =24.5 , 23.0
Next Value
temp =23.0 , 23.5
Next Value
temp =23.0 , 24.0
Next Value
temp =23.0 , 24.5
Next Value
temp =23.0 , 25.0
Next Value
temp =23.0 , 25.5
Next Value
temp =23.0 , 26.0
Next Value
temp =23.0 , 26.5
Next Value
Exception in thread "main" java.lang.ArrayInde xOutOfBoundsExce ption: 10
at Temperature. main(Temperature .java:44)
The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. http://in.yahoo. com/
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]
Java Official Group is created for the following topics: Java 2 Enterprise Edition - J2EE, Java 2 Standard Edition - J2SE, Java 2 Micro Edition - J2ME, XML, XSL, XSD, XPATH, Web Services, Jini, JXTA for all type of Java Geeks.
Whoever posts spam / ads / job related message will be BANNED IMMEDIATELY
No comments:
Post a Comment