Results 1 to 13 of 13
  1. #1
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default 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.

  2. #2
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    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());
            }
        }
    }

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Same idea.... remove this admin....
    freedom exists in the world of ideas

  4. #4
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    Hey sorry I was bored and just got home from work!

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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

  6. #6
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    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.

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    If you wanted a fun way to do it just take powers of 10 from it.
    My IP address is 127.0.0.1

  8. #8
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Thank you for the suggestions everyone.

  9. #9
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    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.

  10. #10
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    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.

  11. #11
    Join Date
    Jun 2008
    Posts
    5
    Rep Power
    0

    Default

    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%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;
    		
    	}

  12. #12
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    That has got to be an easier way of doing this though.

  13. #13
    ferranb is offline Member
    Join Date
    Jun 2008
    Posts
    22
    Rep Power
    0

    Default

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

    Java Code:
            for (int d : intToArray(61235437))
                System.out.print(d);
    Ferran

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •