user3209940
user3209940

Reputation: 3

turning CSV string[] into string[][]

I have turned a grid into a CSV format, which turns it into a string in order to save the grid.

However, when I want to open the CSV file, I have to turn the string back into a 2D array.. I have tried to work out how to do it, but I am unsure on how to join the two string[] so that it turns into a 2D array.

I added an ; where the line ends and a new line must begin, but I am confused about how to add it together.

The code:

public static void open() {
            // The name of the file to open.
            String name = JOptionPane.showInputDialog(null,
                    "Enter the name of the file you wish to open: ");
            String fileName = name+ ".txt";

            // This will reference one line at a time
            String line = null;

            char gridWorld[][];

            try {
                // FileReader reads text files in the default encoding.
                FileReader fileReader = new FileReader(fileName);

                // Always wrap FileReader in BufferedReader.
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                String[] firstsplit, secondsplit;
                while ((line = bufferedReader.readLine()) != null) {
                    for(int i = 0; i < line.length(); i++){
                        firstsplit = line.split(";");   // if semi colon, replace with new line

                    }

                    secondsplit = line.split(","); // splitting the line in columns

                }

                // Always close files.
                bufferedReader.close();

any help would be greatly appreciated.

Upvotes: 0

Views: 78

Answers (2)

Java Devil
Java Devil

Reputation: 10959

If your input csv file is in the misformed

words,things,stuff;more words, more stuff, more things
line2 words,line2 stuff,line2 things; 

So there are multiple lines in the file with ; in the lines which you want to separate into different lines also so that your output would be

words,things,stuff
more words,more stuff, more things
line2 words,line2 stuff,line2 things

What you will need to do is read each line first and splitting it by ; holding these in memory. The below approach will also mean that you will not need to know the size of your grid in advance

ArrayList<String> al = new ArrayList<String>();
while ((line = br.readLine()) != null) {
{
    String[] split1 = line.split(";");
    for( String s1 : split1)
        al.add(s1);
}

String[][] gridWorld = new String[al.size()][];

for(int i = 0; i < al.size(); i++)
{
    gridWorld[i] = al.get(i).split(",");
}

And this can all be reduced to this

ArrayList<String> al = new ArrayList<String>();
while ((line = br.readLine()) != null) {
    al.addAll(Arrays.asList(line.split(";")));

String[][] gridWorld = new String[al.size()][];

for(int i = 0; i < al.size(); i++)
    gridWorld[i] = al.get(i).split(",");

Upvotes: 0

kmera
kmera

Reputation: 1745

The .csv file format with line# and indices:

       | columnIndex
       | 1 2 3 4 
----------------
line1  | 1,2,3,4
line2  | 5,6,7,8
line3  | 9,a,b,c
...
lineN  | w,x,y,z

With this visualization it should be easy to see how to parse it. Here is a code snippet to read it into your gridWorld array, hope this helps:

lineIndex = 0;
while ((line = br.readLine()) != null) {
    String[] split = line.split(",");
    for (int i=0; i<split.length; i++) {
        gridWorld[lineIndex][i] = split[i].charAt(0);        
    }
    lineIndex++;
}

Upvotes: 3

Related Questions