Results 1 to 6 of 6
  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Splitting an string of number into multiple lines

    I got one string test="101010101010101010101010101010101010101010" it's 20x10 and want to split it into one output that shows 5x10's in a line.
    Been thinking on using .charAt() in one for loop but having troubble twisting my ideas on getting it done.

    Been doing java tasks all easter, since the weather have sucked too mutch.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Splitting an string of number into multiple lines

    For subdividing a String at specific lengths, use substring(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Re: Splitting an string of number into multiple lines

    here is what i've gotten into so far, one eased down code cut.

    Java Code:
    class stringedit{
    	public static void main(string[]args){
    		String testString = "20 21 22 23 24 25 26 27 28 29";
    		String finalString=""; // MAX 5 NUMBERS EACH LINE
    		int testlength = testString.length();
    		for (int i=0;i<testlength;i++){
    			finalString = finalString+testString.substring(15)+"\n"
    		}// END FOR
    	}// END MAIN
    }//END STRINGEDIT
    the whole program was to enter 20 numbers in between 20 and 50. store them in one array and print them out in one messagebox with only 5 numbers each line.

    having some troubble on the FOR loop.
    also, are my thoughts so far going the right direction ?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Splitting an string of number into multiple lines

    Where's your array? Will the user be entering numbers separated by spaces? If so, look into String#split(...) to create the String array for you.

  5. #5
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Re: Splitting an string of number into multiple lines

    that was just one example code. had to make it so i could translate into English also.
    I've entered the numbers 20 times seperatly and it's stored in one array.
    and then pulled out of the array and into one string with one " " in between each number so they won't be clustered.
    also made one check that if the number typed in ain't in between 20 and 50 you will get one message and type it in again.

    also trying not to be rude to get people to code for me but sending me in the right direction and maybe one concept code for the current problem at hand.

  6. #6
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Re: Splitting an string of number into multiple lines

    Java Code:
    class stringedit{
        public static void main(string[]args){
            String testString = "20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39";
            String finalString=""; // MAX 5 NUMBERS EACH LINE
            int testlength = testString.length();
            for (int i=0;i<testlength;i++){
                finalString = finalString+testString.substring(i,i+14)+"\n"
    			i+=14;
            }// END FOR
        }// END MAIN
    }//END STRINGEDIT
    and then problem is solved, had to use another forum for some help. might be the good part of internet. =)

Similar Threads

  1. Splitting program into multiple files
    By ourimaler in forum New To Java
    Replies: 3
    Last Post: 02-24-2012, 11:50 AM
  2. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  3. Splitting number
    By cshoya in forum New To Java
    Replies: 2
    Last Post: 06-06-2011, 03:23 AM
  4. String Splitting
    By A.M.S in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 07:17 AM
  5. Replies: 3
    Last Post: 12-17-2007, 02:35 AM

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
  •