Results 1 to 13 of 13
 06062008, 07:03 PM #1Member
 Join Date
 Jun 2008
 Posts
 20
 Rep Power
 0
How do I strip down integers for future manipulation?
Ok, so here is the deal.
Part of an assignment is to take a long number like
1238398173987
and strip it down to
ints like 1 2 3 8 etc...
but we won't be using arrays since we did not learn this yet in class.
So my question is how would I separate the numbers?
Would using modulus do it? if so, would I have to declare all the individual ints?
I need to do this in order to then do various conditional statements to the integers and to add them up.
Suggestions are welcome, thank you.
 06062008, 08:03 PM #2Member
 Join Date
 Jun 2008
 Posts
 43
 Rep Power
 0
Hi here is one way you could do it, and the create Integers from the individual results. You would probably need some validation to make sure the character is actually a numeric value. Hope it gives you some ideas.
Java Code:public class StringSplit { public static void main(String[] args) { int toBeSplit = 123456; String string = Integer.toString(toBeSplit); for(int i = 0; i < string.length(); i++) { Integer integer = new Integer( Integer.valueOf( new Character(string.charAt(i)).toString() ) ); System.out.println("int = " + integer.intValue()); } } }
 06062008, 08:05 PM #3
Same idea.... remove this admin....
freedom exists in the world of ideas
 06062008, 08:07 PM #4Member
 Join Date
 Jun 2008
 Posts
 43
 Rep Power
 0
Hey sorry I was bored and just got home from work!
 06062008, 08:16 PM #5
That's ok, i thought nobody gonna answer this thread at that moment.... My reply is just an algorithm of yours, so i removed it....
Usually, this kind of question needs a code, not an algo... :)freedom exists in the world of ideas
 06062008, 08:20 PM #6Member
 Join Date
 Jun 2008
 Posts
 43
 Rep Power
 0
I was just bored and waiting to go out, I thought it looked like a nice quick problem to solve and I never had to do that before, but it still looked like fun.
 06062008, 09:45 PM #7
If you wanted a fun way to do it just take powers of 10 from it.
My IP address is 127.0.0.1
 06072008, 12:11 AM #8Member
 Join Date
 Jun 2008
 Posts
 20
 Rep Power
 0
Thank you for the suggestions everyone.
 06072008, 11:49 AM #9Member
 Join Date
 Jun 2008
 Posts
 43
 Rep Power
 0
 06072008, 02:40 PM #10Member
 Join Date
 Jun 2008
 Posts
 20
 Rep Power
 0
Yes, that would help. I tried doing it but all I got was the first number on the right, then the last TWO numbers on the right.
 06072008, 04:03 PM #11Member
 Join Date
 Jun 2008
 Posts
 5
 Rep Power
 0
Here's one, returns an integer array containing all the digits of an integer in order.
Probably could be better though.
Java Code:public int[] longSplit(int toSplit){ LinkedList<Integer> ret=new LinkedList<Integer>(); int tens=10; int j=0; while(tens<toSplit*10){ int prev=0; for(int k=0;k<j;k++){ prev+=ret.get(k); } ret.add((toSplit%tensprev)/(tens/10)); tens*=10; j++; } int[] r=new int[ret.size()]; for(int i=0;i<ret.size();i++){ r[i]=ret.get(i); } return r; }
 06082008, 02:27 AM #12Member
 Join Date
 Jun 2008
 Posts
 20
 Rep Power
 0
That has got to be an easier way of doing this though.
 06142008, 02:17 AM #13Member
 Join Date
 Jun 2008
 Posts
 22
 Rep Power
 0
Another nice way:
Java Code:public int[] intToArray(int number) { int size = (int) Math.ceil(Math.log10(number)); int[] digits = new int[size]; double aux = number; for (int i=size1 ; i>=0 ; i) { digits[i] = (int) aux%10; aux /= 10; } return digits; }
Java Code:for (int d : intToArray(61235437)) System.out.print(d);
Similar Threads

Finding Median of X Integers
By Hasan in forum New To JavaReplies: 3Last Post: 08122008, 02:06 PM 
[SOLVED] File Movement/Manipulation
By Leprechaun in forum New To JavaReplies: 2Last Post: 04232008, 12:39 AM 
String manipulation example (Title case)
By Java Tip in forum Java TipReplies: 0Last Post: 01292008, 10:04 AM 
Random Integers
By www.kwalski.com in forum Java AppletsReplies: 8Last Post: 12092007, 06:49 PM 
String Manipulation Task
By hiranya in forum New To JavaReplies: 1Last Post: 11192007, 12:07 PM
Bookmarks