Computer Science Canada

array out of bounds

Author:  cool dude [ Wed Jul 26, 2006 2:01 pm ]
Post subject:  array out of bounds

i made this sample program test however i'm getting an array out of bounds error and i can't seem to fix it.

code:

import java.math.*;

public class Problem25 {
        public static void main (String[] args) {
                BigInteger[] num = new BigInteger[2];
                               
                for (int i = 0; i < 3; i++) {
                        if (i == 0) {
                                num[0] = BigInteger.valueOf(1);
                        }
                        if (i == 1) {
                                num[1] = BigInteger.valueOf(1);
                        }
                        else {
                                num[i] = num[i-1].add(num[i-2]);               
                        }
                }
                System.out.println(num[2]);
        }
}

Author:  rizzix [ Wed Jul 26, 2006 2:30 pm ]
Post subject: 

code:
      BigInteger[] num = new BigInteger[3];


3 not 2. It's the length not the last index.

Author:  [Gandalf] [ Wed Jul 26, 2006 3:53 pm ]
Post subject: 

So to prevent things like this from happening in the future, use num.length instead of a constant number which really has nothing to do with your array. The loop would become:
code:
for (int i = 0; i < num.length; i++) ...


Though this brings up the question of... Do you not understand arrays yet, or did you simply overlook this? Arrays are much more important to understand than BigIntegers.

Author:  cool dude [ Thu Jul 27, 2006 11:09 am ]
Post subject: 

thanks. one more question. how would you get the length of the number of digits in the array. for example if the number was 1234 it would tell me that its 4 digits long. i tried using lenght(); but it doesn't work. it says, "cannot resolve symbol method length()"

Author:  cool dude [ Thu Jul 27, 2006 11:23 am ]
Post subject: 

here's my code:

code:
import java.math.*;

public class Problem25 {
        public static void main (String[] args) {
                BigInteger[] num = new BigInteger[7];
               
                num[0] = BigInteger.ONE;
                num[1] = BigInteger.ONE;
                for (int i = 2; i < num.length; i++) {
                        num[i] = num[i-1].add(num[i-2]);
                        if (length(num[i]) >= 2) {
                                System.out.println(num[i]);          
                        }
                }
        }
}

Author:  wtd [ Thu Jul 27, 2006 11:42 am ]
Post subject: 

Where does the method "length" come from?

Author:  cool dude [ Thu Jul 27, 2006 12:08 pm ]
Post subject: 

wtd wrote:
Where does the method "length" come from?


what do you mean where it comes from? i've always been able to do something like :
code:
num.length();
but because this is an array i can't do it the same way i'm guessing.

Author:  wtd [ Thu Jul 27, 2006 12:24 pm ]
Post subject: 

Back to Java 101.

All methods live inside classes. Which class does the length method live in?

Author:  cool dude [ Thu Jul 27, 2006 12:34 pm ]
Post subject: 

wtd wrote:
Back to Java 101.

All methods live inside classes. Which class does the length method live in?


oh i see. the string class. so now i have to convert the array to a string. but how?

Author:  cool dude [ Thu Jul 27, 2006 1:03 pm ]
Post subject: 

just figured it out. thanks for the help Smile


: