Results 1 to 10 of 10
  1. #1
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Need Help splitting up an Int in a variety of ways

    I'm trying to make a file that divides an Int number (or a Line of digits) in every way possible that consists of single and double digits. I'm mainly having trouble finding an equation that works for both For Loops. I'm using a multidimensional int array to store the splitter variations and the single/double digits (for example split_numbers[splitter][digits]). The file functions will recieve the int number and return an int[][]. Any help on this would be greatly appreciated.

    CURRENT DRAFT
    (spacing is different on this, i originally wrote this on Notepad)
    X = number/digit
    | = splitter placement

    1 = 1
    X|X|X|X|X|X|X|X

    2 = 7
    X|X|X|X|X|X|X X
    X|X|X|X|X|X X|X
    X|X|X|X|X X|X|X
    X|X|X|X X|X|X|X
    X|X|X X|X|X|X|X
    X|X X|X|X|X|X|X
    X X|X|X|X|X|X|X

    3 = 15
    X|X|X|X|X X|X X
    X|X|X|X X|X|X X
    X|X|X X|X|X|X X
    X|X X|X|X|X|X X
    X X|X|X|X|X|X X

    X|X|X|X X|X X|X
    X|X|X X|X|X X|X
    X|X X|X|X|X X|X
    X X|X|X|X|X X|X

    X|X|X X|X X|X|X
    X|X X|X|X X|X|X
    X X|X|X|X X|X|X

    X|X X|X X|X|X|X
    X X|X|X X|X|X|X

    X X|X X|X|X|X|X

    4 = 10
    X|X|X X|X X|X X
    X|X X|X|X X|X X
    X X|X|X|X X|X X

    X|X X|X X|X|X X
    X X|X|X X|X|X X

    X X|X X|X|X|X X

    X|X X|X X|X X|X
    X X|X|X X|X X|X

    X X|X X|X|X X|X

    X X|X X|X X|X|X

    5
    X X|X X|X X|X X

    NEGATIVE | POSITIVE
    (-) | (+)
    D = double digits/split placement
    D
    X|XXXXXXXX
    D D
    X|XXXXXXXX
    D D
    X|XXXXXXXX
    D D
    X|XXXXXXXX
    D D
    X|XXXXXXXX
    D D
    X|XXXXXXXX
    D D
    X|XXXXXXXX
    D D
    X|XXXXXXXX

    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX
    D D D
    X|XXXXXXXX

    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX
    D D D D
    X|XXXXXXXX

    D D D D
    X|XXXXXXXX

    THIS IS THE BASIC STRUCTURE I HAVE FOR SPLITTING AN INT NUMBER
    Java Code:
    int original = 'USER INPUT';
    int mod_digs = original;
    int d1, d2, d3, d4; //double digit placement\
    int ab = 0; //current placement
    
    d1 = d2 = d3 = d4 = -1;
    for ()
    {
    	for ()
    	{
    		
    		//Split up int
    		if (ab == d1 || ab == d2 || ab == d3 || ab == d4)
    			ab++;
    		
    		split_numbers[i][j] = mod_digs / (int) ( 100000000 / Math.pow(10, ab + 1) );
    		mod_digs = mod_digs % (int) ( 100000000 / Math.pow(10, ab + 1) );
    		ab++;
    	}
    	mods_digs = original;
    	
    	if (d1 == 6)
    	{
    		d2++;
    		d1 += d2 + 2;
    	}
    	if (d2 == 4)
    	{
    		d3++;
    		d2 += d3 + 2;
    	}
    	if (d3 == 2)
    	{
    		d4++;
    		d3 += d4 + 2;
    	{
    	d1++;
    	
    	ab = 0;
    }
    Thanks in advanced

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm not entirely sure I understand the problem...

    I'm trying to make a file that divides an Int number (or a Line of digits) in every way possible that consists of single and double digits.

    But this looks like it could be done with recursion. (Counting the number of ways you can climb a staircase taking steps of one or two is a classic example of the fibonacci sequence.)

    To split a sequence:
    * If the sequence has no characters you are finished
    Otherwise
    * take the first character and consider the splits that are possible for the rest of the sequence
    If there are 2 characters also
    * take the first two characters and consider the splits that are possible for the rest of the sequence

    Note this is a sequence-of-characters problem, not a numeric one. You can treat it numerically and go jump all the hoops of extracting digits from numeric quantities but that seems extra work.

  3. #3
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Sorry, I tried making an example with the chart, but i guess its something i already got in my head. For example, if i was to input a number (a, b, and c), it would look like this, each line is a separate digit and each empty line is a different array. This will just be a 4 digit example to shorten it and make it easy.

    INPUT
    a = 1892
    b = 591
    c = 9480

    a example
    1
    8
    9
    2

    18
    9
    2

    1
    89
    2

    1
    8
    92

    12
    92


    b example
    0
    5
    9
    1

    5
    9
    1

    0
    59
    1

    0
    5
    91

    5
    91




    c example
    9
    4
    8
    0

    94
    8
    0

    9
    48
    0

    9
    4
    80

    94
    80

    IF I WAS TO PRINT OUT THE ARRAY, THIS IS WHAT IT WOULD CONSIST OF. SINGLE AND DOUBLE DIGITS ONLY.

  4. #4
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Sorry, you actutally provided the correct answer, at first, I looked up Fibonacci sequencing and didn't find anything simualr, but then realized that it is a different theorem (theorom 2). I figured out how to get my program to read the arrays just fine, but allocating them has been a pain in the a$$. i don't know much as how the equation actually works atm. Still haven't reach that point of math yet :( I'm hopeing someone can hook it up with something resembling the Fibonacci Theorom 2 so i can go from there. I've been searching the internet, but i'm basically screwed for any piece of the theorem 2.

    i'm trying to make an equation that imputes n and returns k. And an equation that inputs n and k to find ??? (not sure what to call it).

    k 0 1 2 3 4 5 6
    n
    0 1
    1 1
    2 1 1
    3 1 2
    4 1 3 1
    5 1 4 3
    6 1 5 6 1
    7 1 6 10 4
    8 1 7 15 10 1
    9 1 8 21 20 5
    10 1 9 28 35 15 1
    11 1 10 36 56 35 6
    12 1 11 45 84 70 21 1

    n = the amount of digits that is in the Int
    k = the amount of variation within the amount of digits (n-i)
    Java Code:
    k = fibonacci(n);
    int[][] splitnumbers = new int[k][];
    for(i = 0; i < splitnumbers[].length; i++)
    {
        int[][] splitnumbers = new int[i]["K + N equation"];
        for (j = 0; j < splitnumbers[i][j].length; j++)
        {
             splitnumbers[i][j] = //numbers from int;
        } 
    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Don't worry about the Fibonacci reference. I was just thinking out loud to justify why I thought the problem looked recursive. (and hence might benefit from using recursion.) The real point I was trying to make is in the little recipe I provided for splitting a sequence.

    Maybe code is easier to understand:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Splitter {
        public static void main(String[] args) {
            String test = "17260";
            for(String str :split(test)) {
                System.out.println(str);
            }
        }
    
        static List<String> split(String s) {
            int len = s.length();
            List ret = new ArrayList<String>();
            if(len == 0) {
                ret.add(s);
                return ret;
            }
            String beginning = s.substring(0, 1);
            for(String str :split(s.substring(1))) {
                ret.add(beginning + " " + str);
            }
            if(len > 1) {
                beginning = s.substring(0, 2);
                for(String str :split(s.substring(2))) {
                    ret.add(beginning + " " + str);
                }
            }
            return ret;
        }
    }

    (This is just off the top of my head - and it just accumlates the splits as strings to be output rather than putting them individually as entries in an array. It is intended to illustrate the recipe I gave before.)

  6. #6
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    Nice. It comes pretty close to what i'm trying to get. A lil unfamiliar with <String> Syntax. I'll post what i have coded tomorrow, i had a brain fart tonight, but right now i'm too tired to clean it up. Plus, tomorrow i'll see if i was sane or not about this brain fart.

    Java Code:
    List<String> ret = new ArrayList<String>();

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The angle brackets (aka generics) are a way of saying, eg, what a list is a list of. So List<String> means a list of strings. It doesn't have to be String: you can have List<Integer> or whatever.

    This pays off with the for loops (although that's not 100% obvious because I used strings) because you don't have to do any casting. The compiler will know that whatever you get() from the list will be a string. Likewise it will complain if you add anything but a string to the list.

    Java Code:
    List<String> ret = new ArrayList<String>();

    I only ever used methods from the List interface on ret so that's what I declared it to be. Of course I have to initialise it with an instance of a concrete class, so I used ArrayList which IS-A List. It is common to see variables declared this way as an interface type and then initialised somewhere as a new instrance of a concrete class.

    What matters though is the recursive meaning of "split": to split something is to remove the first one or two elements and join them with whatever you get by splitting the remainder.
    Last edited by pbrockway2; 11-07-2010 at 08:32 AM.

  8. #8
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    To me, the generics, looks like typecasting. I appreciate you providing some code, but there's still a few things that I知 not familiar with (the different for loop was one of them too), but the fact that you showed me new code opens me up to a new way of coding that痴 got me interested. So, I値l just do some research when I got this program finished and see if I might be able to code it up different. I prefer not to waste someone痴 breathe with something that could simply be goggled :D I mainly come to this site for different viewpoints, to ask questions that I知 stumped on, and to see if I知 over looking something

    When I first made the program, I tried setting it up with strings and using the .substring method, but had trouble turning a string into a int; I have now already learned how, but I had my code going in one direction already.

    Actually, you are right, it is a Fibonacci sequence. I was goggling sequence orders that matched up to mine and I kept getting "Fibonacci Theorem 2". I'm not too worried about getting it integrated into my code, because it would only be used to allocate the arrays. I was always taught to try and use for loops to allocate arrays.

    Well, this is what I知 been working on the past few days when i haven't been working. Any input on this would be great.
    Java Code:
    public class SplitterTest
    {
    	public static void main(String[] args)//params:
    	{
    		int[][] split_numbers;
    		int i, j;
    		i = j = 0;
    		int original_numbers = 12345678;
    		int mod_digs;
    		mod_digs = original_numbers;
    		int byp1, byp2;
    		byp1 = byp2 = 0;
    		
    		/* Sample of code
    		[0][0-7] = 1
    		1
    		X|X|X|X|X|X|X|X
    		
    		[1-7][0-6]
    		2 = 7
    		X|X|X|X|X|X|X X
    		X|X|X|X|X|X X|X
    		X|X|X|X|X X|X|X
    		X|X|X|X X|X|X|X
    		X|X|X X|X|X|X|X
    		X|X X|X|X|X|X|X
    		X X|X|X|X|X|X|X
    		
    		[8-22][0-5]
    		3 = 15
    		X|X|X|X|X X|X X
    		X|X|X|X X|X|X X
    		X|X|X X|X|X|X X
    		X|X X|X|X|X|X X
    		X X|X|X|X|X|X X
    		X|X|X|X X|X X|X
    		X|X|X X|X|X X|X
    		X|X X|X|X|X X|X
    		X X|X|X|X|X X|X
    		X|X|X X|X X|X|X
    		X|X X|X|X X|X|X
    		X X|X|X|X X|X|X
    		X|X X|X X|X|X|X
    		X X|X|X X|X|X|X
    		X X|X X|X|X|X|X
    		
    		[23-32][0-4]
    		4 = 10
    		X|X|X X|X X|X X
    		X|X X|X|X X|X X
    		X X|X|X|X X|X X
    		X|X X|X X|X|X X
    		X X|X|X X|X|X X
    		X X|X X|X|X|X X
    		X|X X|X X|X X|X
    		X X|X|X X|X X|X
    		X X|X X|X|X X|X
    		X X|X X|X X|X|X
    		
    		[33][0-3]
    		5
    		X X|X X|X X|X X
    		 */
    		//TODO MAKE A FOR LOOP THAT HAS AN EQUATION DERIVED IN THEOREM 2
    		split_numbers = new int[34][];
    		
    		
    		split_numbers[0] = new int[8];
    		
    		split_numbers[1] = new int[7];
    		split_numbers[2] = new int[7];
    		split_numbers[3] = new int[7];
    		split_numbers[4] = new int[7];
    		split_numbers[5] = new int[7];
    		split_numbers[6] = new int[7];
    		split_numbers[7] = new int[7];
    		
    		split_numbers[8] = new int[6];
    		split_numbers[9] = new int[6];
    		split_numbers[10] = new int[6];
    		split_numbers[11] = new int[6];
    		split_numbers[12] = new int[6];
    		split_numbers[13] = new int[6];
    		split_numbers[14] = new int[6];
    		split_numbers[15] = new int[6];
    		split_numbers[16] = new int[6];
    		split_numbers[17] = new int[6];
    		split_numbers[18] = new int[6];
    		split_numbers[19] = new int[6];
    		split_numbers[20] = new int[6];
    		split_numbers[21] = new int[6];
    		split_numbers[22] = new int[6];
    		
    		split_numbers[23] = new int[5];
    		split_numbers[24] = new int[5];
    		split_numbers[25] = new int[5];
    		split_numbers[26] = new int[5];
    		split_numbers[27] = new int[5];
    		split_numbers[28] = new int[5];
    		split_numbers[29] = new int[5];
    		split_numbers[30] = new int[5];
    		split_numbers[31] = new int[5];
    		split_numbers[32] = new int[5];
    		
    		split_numbers[33] = new int[4];
    		
    		
    		int ab = 0;
    		//double digit placement
    		int d1, d2, d3, d4;
    		d1 = d2 = d3 = d4 = -1;
    		
    		//goes through all the split variations
    		for (i = 0; i < split_numbers.length; i++)
    		{
    			//stores the digits
    			ab = 0;
    			for (j = 0; j < split_numbers[i].length; j++)
    			{
    				if (ab == d1 || ab == d2 || ab == d3 || ab == d4)
    					ab++;
    				
    				split_numbers[i][j] =  mod_digs / (int) ( 100000000 / Math.pow(10, ab + 1) );
    				mod_digs = mod_digs % (int) (100000000 / Math.pow(10, ab + 1));
    				System.out.println(split_numbers[i][j]);
    				ab++;
    			}
    			
    			//sets up the next pattern
    			d1++;
    			if (d3 >= 2)
    			{
    				d4++;
    				d3 = d4 + 2;
    			}
    			if (d2 >= 4)
    			{
    				if (d3 < 2)
    				{
    					d3++;
    					d2 = d3 + 2;
    					
    				}
    				else
    				{
    					d2 = d3 + 2;
    				}
    			}
    			if (d1 >= 7)
    			{
    				if (d2 == 2 && d3 == 0)
    				{
    					d2 += byp1;
    					byp1++;
    					d1 = d2 + 2;
    				}
    				else if (d2 == 3 && d3 == 1)
    				{
    					d2 += byp2;
    					byp2++;
    					d1 = d2 + 2;
    				}
    				else if (d2 < 4 )
    				{
    					d2++;
    					d1 = d2 + 2;
    					byp1 = 0;
    				}
    				else
    				{
    					d1 = d2 + 2;
    				}
    			}
    			mod_digs = original_numbers;
    			System.out.println("");
    		}
    	}
    }
    Thanks for replying to my post, you've got me researching new ideas. rep++

  9. #9
    JoKeR313 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    I actually looked more into your code, and yours matches up perfectly to what i'm trying to achieve. THANK YOU SO VERY MUCH! Its a different way of producing numbers (at first i didn't see it and didn't understand it (still dont fully understand it)), but it matched up in every way possible. At first i was looking for a function that would split 4,6, & 8 digits and when i checked your code, i didn't think of changing the Int in the code from 5 digit to one of the others; so there were a few things that threw me off. ...but seriously, thank you. i can't thank you enough with the amount of time on this you've taken off me. With what knowledge i had, i couldn't think of a good method.

    I'm definitly going to look into enhanced for loops and mess around with it. It looks like its pretty much the same but cuts alot of code, variables, and combines with generics. i originally thought that there was only one kind of for loop.
    Java Code:
    for (initialization; termination; increment) 
    {
        statement(s)
    }
    This is actually some pretty interesting stuff that i'm looking into, thank you. :D

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're most welcome.

    Do look into the enhanced for loops (and since you referred to them as "enhanced" I guess you have already started doing so). Hopefully the code I posted will become clear enough for you to see that I was trying to illustrate the small recipe I gave in my first post.

Similar Threads

  1. Class to play variety of video formats
    By Swankee in forum New To Java
    Replies: 0
    Last Post: 10-12-2009, 09:38 PM
  2. Replies: 0
    Last Post: 05-31-2009, 08:11 AM
  3. ways to get input from console
    By soni in forum New To Java
    Replies: 2
    Last Post: 05-05-2008, 10:44 PM
  4. Displaying characters in many ways.
    By TampaTechGuy in forum New To Java
    Replies: 7
    Last Post: 01-02-2008, 10:16 PM

Tags for this Thread

Posting Permissions

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