Results 1 to 7 of 7
- 09-30-2011, 04:27 PM #1
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Storing a single number as an array
Hello. I need to store a single number as an array. I will use an array 100 elements long to store the individual digits of a number, and then run arithmetic against it. Addition, subtraction, multiplication, division, modulus, and factorial. The thing is, I can't think of a way to get the number into the array without asking the user to manually enter every digit, which is not acceptable in the context.
Any insight would be greatly appreciated!
- 09-30-2011, 04:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Storing a single number as an array
If using a BigInteger is not an option for you, have a look at the % operator x%10 takes the rightmost digit from the int x and x/10 removes the rightmost digit from the int x.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-30-2011, 04:47 PM #3
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Re: Storing a single number as an array
Oh, that makes a lot of sense actually. But how would i store the integer between moduluses?
- 09-30-2011, 04:54 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Storing a single number as an array
I should leave this up to you, but just because I'm such a nice and friendly person, I give you this pseudo code:
kind regards,Java Code:do { store x%10 in the next array position x= x/10 } while (x != 0);
Jos
ps. I'm moving this thread to the 'New to Java' section because there's nothing advanced about it.When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-30-2011, 10:47 PM #5
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Re: Storing a single number as an array
Thanks! I'll try to make this work.
My apologies, I'm taking what amounts to intermediate java and I wasn't sure where to put it.
- 10-01-2011, 07:22 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Storing a single number as an array
There's no need to apologize; it was my opinion that your question didn't belong in the 'advanced' section so I moved it. None of the other moderators jumped on me so I think they agree. If you have troubles with your code, feel free to post your question(s) here again.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-03-2011, 04:58 AM #7
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Re: Storing a single number as an array
Sorry, disregard this. Edited to inform you I have solved the problem on my own. (I passed it as a String instead, performed a few for loops and some somewhat complicated coding to strip out individual characters from a string, added it to a feeder array, and fed the feeder array to the int array through a toString.)
Thanks for the help everyone!Last edited by Inferno719; 10-03-2011 at 07:33 AM. Reason: Things have changed.
Similar Threads
-
Storing array?
By LifeElixer in forum New To JavaReplies: 7Last Post: 04-14-2011, 06:10 AM -
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM -
Storing a record set into an array
By DJCali in forum New To JavaReplies: 2Last Post: 10-25-2009, 02:47 PM -
Storing in an Array
By Bascotie in forum New To JavaReplies: 10Last Post: 10-15-2009, 05:12 AM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks