Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Splitting integers into parts
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sliverofshadows475




PostPosted: Mon Feb 01, 2010 3:54 pm   Post subject: Splitting integers into parts

Hello,

My teacher recently gave me a challenge problem, and it involves taking a large number and splitting it into individual digits.

ex. 934135 --> 9, 3, 4, 1, 3, 5

I know it has something to do with using remainder (%), but I'm not sure how to go about it.

Any help would be appreciated. Thanks a lot,

-sliverofshadows475
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Feb 01, 2010 3:55 pm   Post subject: RE:Splitting integers into parts

Convert to a string and take each element.
you could do it mathematically I suppose...but it's far easier to cast into a string.
TheGuardian001




PostPosted: Mon Feb 01, 2010 4:07 pm   Post subject: Re: Splitting integers into parts

If you absolutely have to do it mathematically (although I'd also recommend casting to a string), any number mod(%) 10 will return the last digit of that number.
sliverofshadows475




PostPosted: Mon Feb 01, 2010 4:32 pm   Post subject: Re: Splitting integers into parts

So would you just

int num1 = 34;
String str1 = null;


str1 = num1;

? I've only started learning Java this September. Also, I know I'm supposed to put my code into a code box thing, but I don't remember how to do that. Sorry >.>

Also, what would you do once you turned the number into a string?
DtY




PostPosted: Mon Feb 01, 2010 7:16 pm   Post subject: RE:Splitting integers into parts

Might as well do it mathematically, it's exactly what will be done if you do write it into a string, just that you'll be doing explicitly.

- To get the last digit of the number n, do n % 10
- To shift the number so that a different digit is in the last position, divide by multiples of ten, for example 123/10 will give you 12, 123/100 will give you 1

[edit] Took out exact details
SNIPERDUDE




PostPosted: Mon Feb 01, 2010 7:19 pm   Post subject: RE:Splitting integers into parts

The teacher is not likely going to accept the string conversion method. Keep this in mind when you go about coding it:
All base 10 numbers (normal numbers) are exactly that, a base of ten.

So 98765 would be the equivalent of:
(5 * 1) + (6 * 10) + (7 * 100) + (8 * 1000) + (9 * 10000)

Thinking about the logistics, I'd say you would need some form of a counted loop to make sure you the user doesn't require a fixed length integer (ie: you must enter a 5 digit number).
sliverofshadows475




PostPosted: Mon Feb 01, 2010 7:32 pm   Post subject: Re: Splitting integers into parts

So is there a way of determining the length of the number (number of digits), as I wouldn't know what do put for the condition of the for loop?
TheGuardian001




PostPosted: Mon Feb 01, 2010 7:45 pm   Post subject: Re: Splitting integers into parts

I don't know of any way to obtain the length other than to temporarily convert it to a string (although there's probably a better way), and get it's length, IE:

Java:

int length = String.valueOf(1234).length();


Again, there's probably a way that doesn't use strings at all, but this is probably the easiest way, and you'd only be using string to find the length of the number.

Oh, and to put stuff inside of a Java code box, use:
code:

[code="java"]
Java code goes here
[/code]

To use a normal code box, just take out the ="java" part.
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Mon Feb 01, 2010 7:49 pm   Post subject: RE:Splitting integers into parts

I don't think there's any good way of figuring out how many base ten digits are in a number, but, the largest number you can store in a java int is 2147483647, that's ten digits. Since there's no way that you can enter a number larger than this, you can assume that the input is ten digits long, and then strip leading zeroes once it is converted.
sliverofshadows475




PostPosted: Mon Feb 01, 2010 7:51 pm   Post subject: Re: Splitting integers into parts

So basically

Java:

num1 = 45236;

int length = String.valueOf(num1).length();

System.out.println(length);



Will print the length of the integer? Correct me if I'm wrong, but here's the proposed for loop, assuming you can store length in a variable :s

Java:

for (int i = 0; i < length; i++) {
//code here
}
Zren




PostPosted: Mon Feb 01, 2010 8:21 pm   Post subject: RE:Splitting integers into parts

Convert to string and use the length function. Just remember to check for negatives as they'll add a prefix -.

Or you could do it mathematically. Much like finding each digit, you find |n|, then take n and find when |n| - 10^i returns a negative.

n = abs(1234
i = 4
|n| - 10^i = ?
1234 - 10^4 = 1234 - 10 000 = negative

4 digits

There's only one exception you have to watch out for, if n=0, and will return 0 digits. Well actually, it'll result in the first value of i.

Edit: Woops, sorry I get majorly sidetracked and end up posting like half an hours afterwards. Highlighted the important bit.
sliverofshadows475




PostPosted: Tue Feb 02, 2010 9:07 pm   Post subject: Re: Splitting integers into parts

OH ok gotcha! Thanks a lot for all your help guys Very Happy

Thanks,
+sliverofshadows475
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: