Reputation: 35
I have an array that is from .split command and want to put it into an array called String[][] datatabvars, I do not know how to turn datatabvars into a two dimensional array and put the data into it.
public String[] getList() {
String file_name = "path";
String[] links = null;
String[][] datatabvars = null; // this var
int numberOfDatatabs = 0;
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
int i;
for(i=0; i < aryLines.length; i++) { //aryLines.length
if (aryLines[i].substring(0, 7).equals("datatab")) {
aryLines[i] = aryLines[i].replace("datatab["+Integer.toString(numberOfDatatabs)+"] = new Array(", "");
aryLines[i] = aryLines[i].replace(");", "");
datatabvars = aryLines[i].split(","); // this split array
numberOfDatatabs++;
}
}
System.out.println(datatabvars[0]);
}catch (IOException e) {
System.out.println( e.getMessage() );
}
return links;
}
Upvotes: 0
Views: 2083
Reputation: 34367
Update the two lines(I added comment) as below: (I am assuming that rest of your code is working)
String[][] datatabvars = null; // this var
int numberOfDatatabs = 0;
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
datatabvars = new String[aryLines.length][]; // INITIALIZED
int i;
for(i=0; i < aryLines.length; i++) { //aryLines.length
if (aryLines[i].substring(0, 7).equals("datatab")) {
aryLines[i] = aryLines[i].
replace("datatab["+Integer.toString(numberOfDatatabs)+"] =
new Array(", "");
aryLines[i] = aryLines[i].replace(");", "");
datatabvars[i] = aryLines[i].split(","); // this split array: ASSIGNED
numberOfDatatabs++;
}
}
System.out.println(datatabvars[0]);
Upvotes: 3
Reputation: 424993
In general, arrays are to avoided like the plague - use collections if possible:. In this case, split()
returns a String[]
, so use that, but use List<String[]>
to store multiple String[]
:
List<String[]> datatabvars = new ArrayList<String[]>();
...
String[] array = input.split(",");
datatabvars.add(array);
You find life is much easier using collections than arrays.
Upvotes: 1