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