aspose file tools*
The moose likes Beginning Java and the fly likes Obtaining the digits of a integer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Obtaining the digits of a integer." Watch "Obtaining the digits of a integer." New topic
Author

Obtaining the digits of a integer.

Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Let's say someone inputs the number

private int number = 5321;

How could I get every single digit in that number and assign it to an int, or put it into a simple array?

private int[] numbers = {5, 3, 2, 1};

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

You forgot to post your code. What part is giving you a problem?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
I haven't made any code yet, that's what I'm asking you guys about. I just want to know how to take every single digit out of an integer.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

You can either do it the "cheating" way by converting into a String, looping through and using Character.numericValue(s.charAt(n)), or do it a better way by repeatedly dividing by 10 and looking at the result modulus 10.
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Luigi, I appreciate your help a lot, do you think you could give a little more of an example for the second option? Like give some code? I'm sorry, I'm just having some trouble following.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Ben Jass wrote:Like give some code?

You should know by now that that's not how things work here. Please read: NotACodeMill. Are you saying you can't even get started at all? You've already been give two algorithms. Give one of them a try!
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Not a bad start, but think about the hard-coded 5. What if the number is in the millions?
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
My program will only allow 3 digit numbers at the max, so no need. This doesn't even work as it is, it just prints out 1.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

But as a learning exercise (after all, that's why you are here, no?) write the algorithm to handle numbers of any size.
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Ok, that's not that hard, whatever the input, convert to a string, get the length() of that string, convert back to an int and put that as the maximum amount.

That's what I'm not trying to get at though. I don't know how to separate the digits of a number.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Hint: you are expecting a different value for every iteration of your loop. Yet, the loop changes nothing. So how could the result of each iteration change?

My advice: back away from the keyboard and solve the problem with pencil and paper mathematically before trying to write code that mirrors the solution that you come up with.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

You forgot an important part of
Luigi Plinge wrote:by repeatedly dividing by 10 and looking at the result modulus 10.


And you don't have to set the number of iterations in the loop in advance...
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Thanks luigi, I got it.
 
wood burning stoves
 
subject: Obtaining the digits of a integer.