Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-17-2008, 05:09 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
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:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-17-2008, 05:49 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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().
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-17-2008, 06:03 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yep, Roots second option is much better. Make sense to me.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-18-2008, 11:37 PM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
reply
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:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubling the size of an array Java Tip java.lang 0 04-14-2008 09:42 PM
Declaration asifahmed New To Java 1 04-05-2008 06:38 AM
JSP Declaration Directive Java Tip Java Tips 0 12-10-2007 06:42 PM
Changing size of Array ravian New To Java 1 11-14-2007 09:20 PM
Maximum size of an array Hasan New To Java 1 05-20-2007 12:11 PM


All times are GMT +3. The time now is 02:33 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org