Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-06-2008, 09:03 PM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-06-2008, 10:03 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
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.

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()); } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-06-2008, 10:05 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-06-2008, 10:07 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
Hey sorry I was bored and just got home from work!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-06-2008, 10:16 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-06-2008, 10:20 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-06-2008, 11:45 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
If you wanted a fun way to do it just take powers of 10 from it.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-07-2008, 02:11 AM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
Thank you for the suggestions everyone.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-07-2008, 01:49 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
Quote:
Originally Posted by Zosden View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-07-2008, 04:40 PM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-07-2008, 06:03 PM
Member
 
Join Date: Jun 2008
Posts: 5
pjm35@st-and.ac.uk is on a distinguished road
Here's one, returns an integer array containing all the digits of an integer in order.

Probably could be better though.

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%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; }
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-08-2008, 04:27 AM
Member
 
Join Date: Jun 2008
Posts: 20
frasifrasi is on a distinguished road
That has got to be an easier way of doing this though.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-14-2008, 04:17 AM
Member
 
Join Date: Jun 2008
Posts: 22
ferranb is on a distinguished road
Another nice way:

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=size-1 ; i>=0 ; i--) { digits[i] = (int) aux%10; aux /= 10; } return digits; }
To print the result:

Code:
for (int d : intToArray(61235437)) System.out.print(d);
Ferran
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Median of X Integers Hasan New To Java 3 08-12-2008 04:06 PM
[SOLVED] File Movement/Manipulation Leprechaun New To Java 2 04-23-2008 02:39 AM
String manipulation example (Title case) Java Tip Java Tips 0 01-29-2008 11:04 AM
Random Integers www.kwalski.com Java Applets 8 12-09-2007 07:49 PM
String Manipulation Task hiranya New To Java 1 11-19-2007 01:07 PM


All times are GMT +3. The time now is 12:00 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org