0

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;
}

2 Answers 2

3

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]);
Sign up to request clarification or add additional context in comments.

3 Comments

i tried using datatabvars[numberOfDatatabs] = aryLines[i].split(","); // this split shouldn't that create them as needed startign with 0, then goign as high as ti can?
You should definitely not assume the length or initialize the entire array when you then throw away the initialized length-10 array by assigning to datatabvars[i]. Rather, just do datatabvars = new String[aryLines.length][]; initializing just the outer array. The rest of your code works.
@user1816548 Updated the answer. If that is that reason for downvote, please remove.
1

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.

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.