How to save strings from StringSplit to different arrays..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How to save strings from StringSplit to different arrays..

Hybrid View

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    How to save strings from StringSplit to different arrays..

    Hi.. I am having a problem regarding the saving of split words from StringSplit function..
    array output stores my input per line

    my input, for instance is:

    int data_type
    a identifier
    . terminator

    i use:

    for(int i=0;i<=outputlength;i++)
    array[i]=java.util.Arrays.toString(output[j].split(" "));

    then if i print the contents of array[], i get:

    int, data_type
    a, identifier
    ., terminator

    i want to get the first words of lines into an array and the second words into another array,
    for example:
    array a has [int, a, .]
    and array b has [data_type, identifier, terminator]

    Any kind of help will be appreciated, thank you!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to save strings from StringSplit to different arrays..

    You need to store the results of the split method in a 2D array and then create your first and last word arrays and fill them from the 2D array.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: How to save strings from StringSplit to different arrays..

    Maybe you could use the second form of String.split()

    Code:
    String.split(" ", 2); //splits the string into at most 2 elements.
    This would cut your string into the first part (in this case the "int" before the delimiter " " and the rest of the string). You could write a method that writes the first split to the correct array then passes the second string (that is the remainder of the original string) to be parsed in the same way to the other array. Back and forth until the string cannot be split again.

    You also might want to check out the Java API for Patterns.
    http://download.oracle.com/javase/6/...x/Pattern.html

    I took a look at patterns and it seems you might be able to divide you string up into subsets and parse them from groups. I've never done that but it seems like that is what regular expressions are for.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to save strings from StringSplit to different arrays..

    You also might want to check out the Java API for Patterns.
    I think you are over complicating a very simple problem.

    Using split(..) with the second parameter maybe useful if the OP wants to split the string into exactly 2 parts but if he/she wants to extract the first 2 words then just use the single parameter version.

    All you need is a simple loop:

    Code:
            String[] input = {"my line", "some text", "more words to use"};
            String[][] array = new String[input.length][];
        
            // split the lines
    	for ( int i = 0; i < input.length; i++ )
    		array [i] = input[i].split(" ");    
        
    	String[] w1 = new String[array.length];
    	String[] w2 = new String[array.length];
    	
            // separate the array into arrays of first and second words
    	for ( int i = 0; i < array.length; i++ )
    	    {
    	    w1[i] = array[i][0];
    	    w2[i] = array[i][1];
    	    }
    	
    	System.out.println("W1 = "+Arrays.toString(w1));
    	System.out.println("W2 = "+Arrays.toString(w2));
    Of course this does make the massive assumption that there is at least 2 words in each input line.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2011
    Posts
    2

    Re: How to save strings from StringSplit to different arrays..

    @keang It worked! Thank you!

    @djgarrison Thank you too!

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center