• Post Reply Bookmark Topic Watch Topic
  • New Topic

identifying parts of a larger number  RSS feed

 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a number like 47, is there a way to extract out each individual number from that?
I'm trying to get a 4 and a 7.

I searched through "number" in the API and didn't see anything that mirrored that. I'm not sure what else I'd google for.

Thanks for any guidance.
Matt
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turn the number into a string and use charAt() to get the individual numbers. Then you can use parse int to get them back to an int if needed.
[ April 05, 2005: Message edited by: Hentay Duke ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't anything in the API that will specifically do this. You will most likely need to write your own code to do it instead. One important fact that you didn't mention is how the number is represented. Is it an int or a float or a String or something else? If it's a String, you can use the suggestion above. Even if it's an int or float, you can easily convert it to a String. However, there's an easier way if you understand integer arithmetic. Specifically, you should look at integer division and modulus operations. I won't give any more details because I think you should research this on your own. If you need any clarifications from there, let us know.

Layne
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number would be an integer to start.
I didn't think of parsing to String and I see how that could work.

If I understand you Layne, you are implying to use something like:

if (( 47 % 40 ) < 10 )...
The remainder would then be the int in the ones place.

I just figured that would be a whole mess of testing.
Is that similar to what you implied?

That's funny. After looking at this a bit, that could work well (I think).
Does that make sense?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's more that you need to use both integer division and modulus together.

What is 47/10? (in integer division)
What is 47%10?
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you always know the number ahead of time? If so then something like could work. What if you don't know the number ahead of time (of course if you do know the number ahead of time then you don't need code do you)? What will be the second operand of the your mod expression if the number entered is 41506? Do you need just one number at a time or sometimes the first two numbers, etc...?
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hentay Duke:
Will you always know the number ahead of time? [...] What if you don't know the number ahead of time... [...]


I agree, but didn't feel like typing out code. I was just trying to convey the idea and make sure I was on track with using the remainder etc.

Sometimes I just need a little nudge in the right direction. Thanks everybody. I'll put it to code when I get home.
[ April 05, 2005: Message edited by: Matt Fielder ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are almost there, Matt. However, what if you have a variable x and x%40 is greater than 10? What will you do then? It seems like you would need to do a long chain of if...else statements. I think this is what everyone else is trying to point out. So can you figure out a way to get the one's digit with a SINGLE modulus operation, without any if statements or other control structure? (Hint: go back and read Joel's post again.)
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah that was kinda stupid.
I guess

47 / 10 = 4
and
47 % 10 = 7

would get me there faster without several if tests.
I instantly get the two int values in 47.

Thanks for the guidance.
Matt
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!