Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java array calculation

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java array calculation

    Would this be the proper way to calculate each elements of an array? The []speed will be import in from a file. There a total of 59 speed thus I have [59]. Then I want to convert the speed ( Which is in knot to mile). I keep getting "java.lang.ArrayIndexOutOfBoundsException: 59"
    when I run the program at the line " mile[v] = (int)((speed[v]) * 1.15);" Thanks

        int[] speed = new int[59];
                int[]mile = new int[59];
               for(int v = 0 ; v <= 60;v++)
           {       
               mile[v] = (int)((speed[v]) * 1.15);
           }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Java array calculation

    Array indexes range from 0 to the length-1. The max index for a 59 element array would be 58.
    Change the for loop values so the index's value does not go past 58.

    If you can't tell what the values are, add a println statement immediately after the for statement that prints out the value of v.

    The for statement should use the array's .length attribute to control the looping, not a magic/hardcoded number.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java array calculation

    Oh. I forgot the different between length and index. Thanks for the help!

Similar Threads

  1. simulate calculation ?
    By meryqat in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2012, 01:03 PM
  2. Java Calculation Incorrect
    By cow23 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 07:09 AM
  3. Calculation error...very New to Java..please be kind.
    By medicinaluser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:16 PM
  4. I need calculation
    By Swiss518 in forum Loops & Control Statements
    Replies: 7
    Last Post: January 27th, 2011, 12:26 PM
  5. RPM Calculation
    By fobos3 in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 21st, 2010, 08:53 AM