
-----------------------------------
cool dude
Wed Jul 26, 2006 2:01 pm

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.


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]);
	}
}

-----------------------------------
rizzix
Wed Jul 26, 2006 2:30 pm


-----------------------------------
      BigInteger[] num = new BigInteger[3];

3 not 2. It's the length not the last index.

-----------------------------------
[Gandalf]
Wed Jul 26, 2006 3:53 pm


-----------------------------------
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:
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.

-----------------------------------
cool dude
Thu Jul 27, 2006 11:09 am


-----------------------------------
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()"

-----------------------------------
cool dude
Thu Jul 27, 2006 11:23 am


-----------------------------------
here's my 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]);		
			}
		}
	}
}

-----------------------------------
wtd
Thu Jul 27, 2006 11:42 am


-----------------------------------
Where does the method "length" come from?

-----------------------------------
cool dude
Thu Jul 27, 2006 12:08 pm


-----------------------------------
Where does the method "length" come from?

what do you mean where it comes from? i've always been able to do something like : num.length(); but because this is an array i can't do it the same way i'm guessing.

-----------------------------------
wtd
Thu Jul 27, 2006 12:24 pm


-----------------------------------
Back to Java 101.

All methods live inside classes.  Which class does the length method live in?

-----------------------------------
cool dude
Thu Jul 27, 2006 12:34 pm


-----------------------------------
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?

-----------------------------------
cool dude
Thu Jul 27, 2006 1:03 pm


-----------------------------------
just figured it out. thanks for the help  :)
