To convert a CSV file with 1 column x thousands of rows into a two-dimensional array

Asked 2 years ago, Updated 2 years ago, 36 views

2 Answers

data[row]=line.split(", ", 0);

I think there is a problem with .

The split method divides a string into multiple strings, separated by a separator character (comma in the code above).
The second argument of the split method specifies the number of post-split strings to return.
But since it's 0, it means no string is returned, and null is returned.

data[row]=line.split(",");

Try changing to .

==
Supplemental
==
CSV is defined as "comma-separated values" (comma-separated values)value), or abbreviation for "comma-separated vector" (comma-separated vector), which usually contains commas in each row of a CSV file.

However, the data in the question only has a string that does not contain commas in each line.
Therefore, it is meaningless to separate the strings (such as "abase") with commas.

Once you've read one line, you can simply insert the contents of the CSV file into the array data by repeating that you want to replace it with the elements of the array.


2022-09-30 18:55

If it is a file with "thousands of lines", it is not possible to declare the variable String[][]data=newString[2][2];.
Also, since the number of rows is probably not fixed, wouldn't it be better to store them on the ArrayList?

I think you should follow the steps below.
Assume that the original text data is paired with consecutive odd and even lines.

For example, the content of the article is called as a subroutine for reading a text file of the number of variable lines.
Create ArrayList from a text file (30 minutes)

ArrayList<String>textLines=newArrayList<String>();;
boolean result=false;
result=readTextFileLines("sample.csv", textLines);

If the above result is an odd number of lines, add an empty string or something to the ArrayList indicating invalid data.

 if(textLines.size()%2==1){
    textLines.add("");
}

Declare a 2D array using ArrayList size().

introcount=textLines.size()/2;
String [ ] [ ] data = new String [rowcount ] [2];

Copy data from ArrayList into a 2D array.

for(introw=0;row<rowcount;row++){
    data[row][0] = textLines.get(row*2);
    data[row][1] = textLines.get(row*2+1);
}

As soon as the created two-dimensional array data is displayed, the humming distance is examined, and some processing is performed.

If the assumption of how to place the original text data in a two-dimensional array changes, the processing in 2.3.4. will change accordingly.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

US>class Sample {

    public static void main(String args[]){
        try{
            ArrayList<String>textLines=new ArrayList<String>();
            boolean result=false;
            result=readTextFileLines("sample.csv", textLines);
            if(result==false){
                return;
            }
            if(textLines.size()%2==1){
                textLines.add("");
            }
            introwcount=textLines.size()/2;
            String [ ] [ ] data = new String [rowcount ] [2];
            for(introw=0;row<rowcount;row++){
                data[row][0] = textLines.get(row*2);
                data[row][1] = textLines.get(row*2+1);
            }
            for (introw=0; row<data.length;row++){
                for (int col=0; col<data.length;col++){
                    System.out.println(data[row][col]);
                }
            }
        } catch(Exceptione){
          System.out.println(e);
        }
    }

    public static boolean readTextFileLines (String filePath, ArrayList<String>textLines)
    {
        boolean result=false;
        FileReader fr=null;
        BufferedReader br=null;
        try
        {
            fr = new FileReader (filePath);
            br = new BufferedReader (fr);
            String line = br.readLine();
            while(line!=null)
            {
                textLines.add(line);
                line=br.readLine();
            }
            result=true;
        }
        catch(FileNotFoundExceptione)
        {
            System.out.println(e);
        }
        catch(IOExceptione)
        {
            System.out.println(e);
        }
        finally
        {
            try
            {
                if(br!=null){
                    br.close();
                }
                else if (fr!=null) {
                    fr.close();
                }
            }
            catch(IOExceptione)
            {
                System.out.println(e);
            }
        }
        return result;
    }
}


2022-09-30 18:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.