简体   繁体   中英

How do I add missing data into ArrayList<String[]>() in Java

I have a .csv file with occasional missing data in 3rd column of a multiple column table. I have read it into Java using CSVReader. In wanting to clean the data first, I need to allocate "unknown" into empty positions.

List <String []> file = new ArrayList <String[]>();

try 
{
    reader = new CSVReader(new FileReader(fileM),',', '"');
    String [] nextLine; 
    while ((nextLine = reader.readNext()) != null) 
    {
        if(nextLine[2].isEmpty())
        {
            nextLine[2] = new String("unknown");
        }
        file.add(nextLine);
    }
    reader.close();

Etc, etc Java throws exception to this every time it comes to the first empty cell at nextLine[2] and won't let me update the empty cell. I think this might be as the Array List is fixed when read in, so how do I add additional strings into specific positions. Or is there a better way to manipulate this in Java?

Apart from isEmpty() you should also check for isNUll(). Checking for null may solve your problem. For checking both condition you can use apache commons StringUtil class.

只需在每次迭代中检查nextLine.length> 0(或nextLine.length == EXPECTED_COLUMNS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM