Results 1 to 15 of 15

Thread: [RESOLVED] Split String into Array

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Location
    England
    Posts
    61

    Resolved [RESOLVED] Split String into Array

    Hey,

    I've written this code to split the string defined in 'encodedPasswd'.

    Any idea how I would get each segment into seperrate Arrays which can be called in another part of the code?

    Code:
    String encodedPasswd = "00f1,0021,0041,0076,0007,00a7,00c7,00f1";
    String split[] = encodedPasswd.split(",");
    for (int cnt = 0; cnt < split.length; cnt++)
    {
    System.out.println(split[cnt]);	
    }
    Cheers for your help guys
    Last edited by DonCash; Jul 11th, 2007 at 06:26 AM. Reason: Resolved that mother!

  2. #2
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Split String into Array

    What I would do is tokenize the input encodedPasswd. With that you know how many arrays you need, then you can dynamically create as many arrays as you need and put in the split text.

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Split String into Array

    Why not use charAt() and treat the String as an array?

    Code:
            String st = "thisisatest";
            for (int index=0; index<st.length(); ++index)
            {
                System.out.println(st.charAt(index));
            }

  4. #4
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Split String into Array

    If you want to store them in a separate arrays, as System_Error says detect the , sign to split them.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Split String into Array

    Any idea how I would get each segment into seperrate Arrays which can be called in another part of the code
    I think it's the String.toCharArray you are looking for

    You can go for a 2D array of chars to store each part as an array
    Last edited by ComputerJy; Jul 9th, 2007 at 03:51 AM.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Split String into Array

    Why? Until found the , sign store each of the previous element in an array.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2007
    Location
    England
    Posts
    61

    Re: Split String into Array

    Hey,

    I have written this code as a test... Basically it splits the encodedPasswd string and returns only the first value 00f1.

    I have included an if statement to say if the result is 00f1 then print 'A' else print 'B'.

    For some reason, even though the value is 00f1, it returns B.

    System.out.println(passBit); returns 00f1 so I don't understand?! Any ideas?

    Please forgive me, I am new to Java.

    Code:
    	public static void main(String[] args) {
    
    		String encodedPasswd = "00f1 0021 0041 0076 0007 00a7 00c7 00f1";
    		String[] split = encodedPasswd.split(" ");
    
    		
    		for (int cnt0 = 0; cnt0 < split.length - 7; cnt0++) {
    		
    		String passBit = (split[cnt0]);
    				
    	
    		if (passBit == "00f1") {
    			    System.out.println("A");
    			} else if (passBit != "00f1") {
    			    System.out.println("B");
    			}
    		
    			System.out.println(passBit);
    	
    
    			
    		}
    			
    	}
    }
    Last edited by DonCash; Jul 9th, 2007 at 09:38 AM.

  8. #8
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Split String into Array

    Quote Originally Posted by DonCash
    Code:
    		
    		for (int cnt0 = 0; cnt0 < split.length - 7; cnt0++) {
    		
    		String passBit = (split[cnt0]);

    Think that can this code hold a string? I think better to store them an array.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Split String into Array

    Quote Originally Posted by eranga262154
    Think that can this code hold a string? I think better to store them an array.
    Code:
    String[] split = encodedPasswd.split(" ");
    They are stored in an array
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Split String into Array

    Then what happened on that code segment I mentioned on my previous post?
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  11. #11
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Split String into Array

    Quote Originally Posted by eranga262154
    Then what happened on that code segment I mentioned on my previous post?
    An iteration through the array
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  12. #12
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Split String into Array

    Ya, it is correct. Iteration through the array. But it is done by a letter?
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2007
    Location
    England
    Posts
    61

    Re: Split String into Array

    Any ideas why its not returning 'A' even though the string is '00f1' ?

    String passBit = (split[cnt0]); Prints 00f1 but for some reason the code doesnt seem to think thats the output in the if statement.

  14. #14

    Thread Starter
    Member
    Join Date
    Jun 2007
    Location
    England
    Posts
    61

    Thumbs up Re: Split String into Array

    Just figured it out,

    It seems to work fine when I use this instead:

    Code:
    			if (passBit.equals( "00f1" )) {
    			System.out.println("A");
    			}
    			else {
    			System.out.println("B");		
    			}

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2007
    Location
    England
    Posts
    61

    Re: Split String into Array

    I'm currently splitting the encodedpassword string using the , delimiter.

    Do you guys know how to make this split after every 4 characters instead?

    I need to turn:

    00f1002100410076000700a700c700f1

    into

    00f1 0021 0041 0076 0007 00a7 00c7 00f1



    ------------------------------------------------------------

    After much playing around and forum hunting i've solved the problem.

    I've used this code:

    Code:
    	String encodedPasswd = "00f1002100410076000700a700c700f10";
    	      
    	 assert encodedPasswd.length() % 4 == 0;
    	 String[] splitLine = new String[encodedPasswd.length()/4];
    	 
    	 for (int index = 0; index < splitLine.length; index++)
    	 splitLine[index] = encodedPasswd.substring(index*4,index*4 + 4);
                  
                  System.out.println(splitLine[0]);
                  System.out.println(splitLine[1]);
                  System.out.println(splitLine[2]);
                  System.out.println(splitLine[3]);
                  System.out.println(splitLine[4]);
                  System.out.println(splitLine[5]);
                  System.out.println(splitLine[6]);
                  System.out.println(splitLine[7]);
                  System.out.println(splitLine[8]);
                  System.out.println(splitLine[9]);
    Lovely
    Last edited by DonCash; Jul 11th, 2007 at 06:25 AM. Reason: Sorted!!

Posting Permissions

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



Click Here to Expand Forum to Full Width