# Thread: ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

1. Member
Join Date
Aug 2014
Posts
4
Rep Power
0

## ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

Hi All,

I have a simple code for temperature conversion using single dimensional array, but I am getting ArrayIndexOutOfBoundsException error.
Please see the code below and help me. I am a beginner in Java

import java.io.*;
class arraycelcius
{
public static void main()throws IOException
{
double f[] = new double[10];
double c[] = new double[10];
int b,e,a;
for(a=0;a<10;a++)
{
System.out.println("Enter the temperature in farenheit");
}
for( b=0;b<10;b++);

c[b]=(f[b]-32/9)*5;

for (e=0;e<=10;e++);
{
System.out.println("The temperature in celcius is "+c[e]);
}}
}

java.lang.ArrayIndexOutOfBoundsException: 10
at arraycelcius.main(arraycelcius.java:18)

2. ## Re: ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

java.lang.ArrayIndexOutOfBoundsException: 10
at arraycelcius.main(arraycelcius.java:18)
At line 18 the code used an index that was out of bounds. Remember that array indexes range in value from 0 to the array length-1.

When indexing through the elements in an array, it is better to use the .length field of the array to control the looping instead of a hardcoded number:
Java Code:
`  for(int i=0; i < theArray.length; i++) {`
Also array indexes should be local to the loop (see i above), not defined outside of the loop like the code does for a, b and e

Please edit your post and wrap your code with code tags:
[code]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
Last edited by Norm; 08-31-2014 at 07:58 PM.

3. ## Re: ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

Java Code:
`for( b=0;b<10;b++); <--- Hmmmm!`
Your loop does nothing except increment variable b to 10 when it exits. It then executes the next line
Java Code:
`c[b]=(f[b]-32/9)*5;`
which is not inside the loop. Therefore trying to access array c at element 10 causes it to fail.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

And when you fix the last loop, change <=10 to <10. Or better still, do what Norm suggested.

Regards,
Jim

5. Member
Join Date
Aug 2014
Posts
4
Rep Power
0

## Re: ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

the user enters 10 number in this case how will i use .length function

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

Originally Posted by sharadjain
the user enters 10 number in this case how will i use .length function
You need to explain the intended operation of your program. What is the user entering? Why do you
need to use an array? Where is the temperature data coming from?

Regards,
Jim

7. ## Re: ArrayIndexOutOfBoundsException in a Single Dimensional Array for temp conversion

An array's length field holds the number of elements in an array. It is usually used in for loops to detect when the index's value is at the end of the array: theArray.length-1
See post#2

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•