|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-06-2008, 09:03 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 20
|
|
|
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.
|
|

06-06-2008, 10:03 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
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.
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());
}
}
}
|
|

06-06-2008, 10:05 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
Same idea.... remove this admin....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-06-2008, 10:07 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
|
Hey sorry I was bored and just got home from work!
|
|

06-06-2008, 10:16 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
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... 
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-06-2008, 10:20 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
|
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.
|
|

06-06-2008, 11:45 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
If you wanted a fun way to do it just take powers of 10 from it.
__________________
My IP address is 127.0.0.1
|
|

06-07-2008, 02:11 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 20
|
|
|
Thank you for the suggestions everyone.
|
|

06-07-2008, 01:49 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
Originally Posted by Zosden
If you wanted a fun way to do it just take powers of 10 from it.
Hi can you a give java code snippet, that would be helpful.
|
|

06-07-2008, 04:40 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 20
|
|
|
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.
|
|

06-07-2008, 06:03 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 5
|
|
Here's one, returns an integer array containing all the digits of an integer in order.
Probably could be better though.
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%tens-prev)/(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;
}
|
|

06-08-2008, 04:27 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 20
|
|
|
That has got to be an easier way of doing this though.
|
|

06-14-2008, 04:17 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 22
|
|
Another nice way:
public int[] intToArray(int number) {
int size = (int) Math.ceil(Math.log10(number));
int[] digits = new int[size];
double aux = number;
for (int i=size-1 ; i>=0 ; i--)
{
digits[i] = (int) aux%10;
aux /= 10;
}
return digits;
}
To print the result:
for (int d : intToArray(61235437))
System.out.print(d);
Ferran
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|