Results 1 to 7 of 7
  1. #1
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default 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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default 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?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: Storing a single number as an array

    Quote Originally Posted by Inferno719 View Post
    Oh, that makes a lot of sense actually. But how would i store the integer between moduluses?
    I should leave this up to you, but just because I'm such a nice and friendly person, I give you this pseudo code:

    Java Code:
    do {
       store x%10 in the next array position
       x= x/10
    }
    while (x != 0);
    kind regards,

    Jos

    ps. I'm moving this thread to the 'New to Java' section because there's nothing advanced about it.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: Storing a single number as an array

    Quote Originally Posted by Inferno719 View Post
    My apologies, I'm taking what amounts to intermediate java and I wasn't sure where to put it.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default 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

  1. Storing array?
    By LifeElixer in forum New To Java
    Replies: 7
    Last Post: 04-14-2011, 06:10 AM
  2. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  3. Storing a record set into an array
    By DJCali in forum New To Java
    Replies: 2
    Last Post: 10-25-2009, 02:47 PM
  4. Storing in an Array
    By Bascotie in forum New To Java
    Replies: 10
    Last Post: 10-15-2009, 05:12 AM
  5. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 07:32 AM

Posting Permissions

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