0

Lets say I have 1d array

String teams[]={"Michael-Alex-Jessie","Shane-Bryan"}; 

and I want to convert it to multidimensional array which will be like this

String group[][]={{Michael,Alex,Jessie},{Shane,Bryan}}. 

I try to use delimeter but for unknown reason I cannot assign the value of 1d to 2d array. It said incompatible types. Please any help will be much appreciated. Here is my code.

String [][]groups ; 
String teams[]={"Michael-Alex-Jessie","Shane-Bryan"};
int a=0,b=0;
String del ="-/"; 

    for (int count = 0; count < teams.length; count++)
    {
       groups[a][b] = teams[count].split(del);
       a++;
    }
2
  • You've declared your 2D array, but you haven't initialized it (i.e. groups = new String[x][y]) - also, whenever asking a question where you're getting an error message, you want to post the error message as well. Commented Dec 22, 2011 at 14:46
  • Yes thanks for reminding me.The error occur at groups[a][b] = teams[count].split(del);When I'm assigning the value of 1d to 2d array and it said incompatible type required java.lang.string found java.lang.string[]. Commented Dec 22, 2011 at 14:54

4 Answers 4

3

Type of group[a][b] is String but you are attempting to assign string array (i.e. String[]) there.

This is what you really want to do:

for (int count = 0; count < teams.length; count++) {
   groups[count] = teams[count].split(del);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Do you know any better idea to place them in multidimensional array?
0

Yoy can't assign array of Strings returned by "teams[count].split(del)" to String

public class qq {
String[][] groups;
String teams[] = { "Michael-Alex-Jessie", "Shane-Bryan" };
int a = 0, b = 0;

public void foo() {
    String del = "-/";
    groups = new String[teams.length][];
    for (int count = 0; count < teams.length; count++) {
        groups[count] = teams[count].split(del);
        a++;
    }
}
}

2 Comments

Is there any other way where I can place the value in multidimensional array?
@user1110191 - this algorithm is so silly and primitive so I can't understand what do you mean by saying "better way"
0

You need to assign an array value, not a cell value:

   groups[count] = teams[count].split(del);

Comments

0

String.split() return an Array of String, not a String

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.