Results 1 to 7 of 7
  1. #1
    sharadjain is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default 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
    {
    InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(read);
    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");
    f[a]= Double.parseDouble(br.readLine());
    }
    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. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    sharadjain is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default 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. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

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

    Quote Originally Posted by sharadjain View Post
    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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. two dimensional array
    By grace425 in forum New To Java
    Replies: 2
    Last Post: 02-23-2013, 12:01 AM
  2. Two dimensional array
    By niu_niu in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 12:34 AM
  3. Compare Array and setting to temp
    By saqib15 in forum New To Java
    Replies: 0
    Last Post: 02-17-2010, 06:16 PM
  4. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 07:21 PM

Posting Permissions

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