4

I would like to know how I can separate a string and assign it to a specific row of a 2d array automatically in the same fashion that the single dimensional array is instantiated below:

public class TestArray {
  public static void main (String [] args) {

    String file1description= "We went to the mall yesterday.";
    String[] file1tags;
    String delimiter= " ";
    file1tags = file1description.split (delimiter);
    for (int i = 0; i < file1tags.length; i++) {
      System.out.println (file1tags[i]);
    }
  }
}

If there are any simpler ways please share. As you can tell I am quite a novice, but I am willing to learn. In this example, each word is separated by the delimiter and stored automatically into the "file1tags" array. How can I do this with a 2d array, so that I can call multiple arrays that have this same function? Thanks in advance!

3 Answers 3

1
public static String [][] to2dim (String source, String outerdelim, String innerdelim) {

    String [][] result = new String [source.replaceAll ("[^" + outerdelim + "]", "").length () + 1][]; 
    int count = 0;
    for (String line : source.split ("[" + outerdelim + "]"))
    {
        result [count++] = line.split (innerdelim);
    }
    return result;
}

public static void show (String [][] arr)
{
    for (String [] ar : arr) {
        for (String a: ar) 
            System.out.print (" " + a);
        System.out.println ();
    }
}   

public static void main (String args[])
{
    show (to2dim ("a b c \n d e f \n g h i", "\n", " "));
}

newbie friendly:

public static String [][] to2dim (String source, String outerdelim, String innerdelim) {
    // outerdelim may be a group of characters
    String [] sOuter = source.split ("[" + outerdelim + "]"); 
    int size = sOuter.length;
    // one dimension of the array has to be known on declaration:
    String [][] result = new String [size][]; 
    int count = 0;
    for (String line : sOuter)
    {
        result [count] = line.split (innerdelim);
        ++count;
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot, but is there a newbie version of this?
@user1299661: Yes, now there is, and a corrected original. "[ \t]" is a regex for blank or tab, for example - a group of possible delimiters can be defined that way.
Thank you so much for your time and patience! I greatly appreciate it!
I was unable to compile your code.3 errors found: Error: Syntax error on tokens, ClassHeader expected instead Error: Syntax error on token ";", { expected after this token Error: Syntax error, insert "}" to complete ClassBody
Well, you have to provide a class header (I'd suggest: public class To2DimStringArray {), an import java.util.*; before that, and if you use the so called newbie-code, the main and show method, as well as a closing brace for the class.
1

here's a simple example


String example = "a b c \n d e f \n g h i";
String[] rows = example.split("\n");
for (int i = 0; i < rows.length; i++) {
  String[] columns = rows[i].split(" ");
  for (int j = 0; j < columns.length; j++) {
    //do something
  }
} 

3 Comments

sry it got tripped up on my less thans! Didn't know I had to html encode them
Where did the variable lines get instantiated?
sry in my previous edit I changed lines to rows, but I didn't make the change everywhere
0
String example = "a b c \n d e f \n g h i";
String[] rows = example.split("\n");
String[][] table;
for (int i = 0; i < rows.length; i++) {
  table[i] = rows[i].split(" ");
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.