Thread: Array size declaration

1. Array size declaration

I'm having a runtime problem with a null pointer, because i didn't create an array (defined it but didn't do new int[number] thing) because i wouldn't know how big the array would be until after the while loop after it, which uses the array. i'm just posting the method since you need nothing more to help me if you can. its the main method ( not main(String[] args), just the main one this class uses, it doesn't have a public static void main) called getFactors and uses primarily bigdecimals, another class uses this one (pretty much just this method) to allow a user to type a number and get all its factors.You can probabaly see where this is going. I looked at the varargs thing but i dont think that's going to help.technically speaking, IT cant know until THAT uses IT to find out what IT needs to run THAT. I have no clue how to solve this and would greatly appreciate help. here is the code:
Java Code:
```
public int[] getFactors(BigDecimal number) {   // the argument is the user entered number
int[] factors;  // i could write int[] factors = new int[but i dont know what goes here yet];
int i = 1;      // use this to initialize the bigdecimal with an int
BigDecimal bdi = new BigDecimal(i); // the count, current number it sees if is a factor then moves on
BigDecimal step = new BigDecimal(1); // the step to add (like i++ but i need to add another bigdecimal)
int aIndex = 0;                      // the current index of array its adding to. only increases when a new value has been added
while ( bdi.intValue() <= number.intValue() ) {     // while the count is less then the user entered number...
if ( number.remainder(bdi).intValue() == 0 ) {  // if there isn't a remainder of the count going into the u-e number
factors[aIndex] = bdi.intValue();           // array index:current index value variable equals the count
bdi.add(step);                              // move on to next number
aIndex++;                                   // increase the arrayindex (since we just put a value int he current one)
}
else {
bdi.add(step);                              // if its not a factor just keep going
}
}
return factors;                                    // return the array of factors
} // end of getFactors method```

2. I will give some pointers to work on,

You have two choices (Which indirectly is the same). You start with an array of length arrayLength and as your aIndex exceeds arrayLength resize the array (System.arrayCopy). Otherwise you can directly use java.utlil.ArrayList.

I havent looked on your algorithm, but on a quick note, Why did you use BigDecimal ? and does your program work correctly when your number in BigDecimal is larger than Integer.MAX_VALUE, Specially in situation where you are depending on intValue().

hi, ya i changed it alot, but now it compiles but at runtime when the number is entered it just looks as if its waiting for more input except typing doesn't work and i have to close command prompt..i used arraylist and i ferget why i used bigdecimals... but ya here is the code:
Java Code:
```
public int[] getFactors(int number) {    // number is the num to get the factors from
ArrayList fList = new ArrayList(2);  // arraylist to use
int[] factors;						 // array to put the arraylist in
int j = 1;							 // counter no. 1 (sumwhere else in the program uses i)
Integer k;							 // counter no. 1 (object form so i can put it in the arraylist)
int l = 0;							 // counter no. 2
while ( j <= number ) {				 // while the factor test for true number is less or equal to the number
k = new Integer(j);				 // counter 1 object is same as int counter
if ( number % j == 0 ) {         // if its a factor
fList.ensureCapacity(j);	 // make loads of room
if ( fList.contains(k) ) {	 // if it already has it
j++;					 // move on
}
else {						 // if not,
fList.add(k);			 // add the object(Integer) count(dont know why i cant put an int into it)
j++;					 // increase counter
}
}
else {							 // if it isn't a factor
j++;						 // move on
}
fList.trimToSize();				 // trim the size of the list
}
factors = new int[fList.size()];	 // factors = enough room for the list
while ( l < fList.size() ) {		 // while counter no.2 is less then size (not <= cause size starts 1 but index starts 0
factors[l] = Integer.parseInt(fList.get(l).toString()); // current factor array index is parsedint of the list object toString
}
return factors;						 // return the factors
} // end of getFactors method```
the end part ( weird parseint of tostring of object of list thing) was built up cause i kept getting compiler errors.. when it was just current factor index = curent list index i got expected int got object compile error. when i changed all ints to integers and once all to objects i got errors.. when i had parseint it doesn't have a method to parseint objects, so i toString'ed them. i'm pretty confused with this, its all an annoying series of variable Type errors. If you can point me to the right direction, please do.

Posting Permissions

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