0

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?

Rachle_S
  • 17
  • 1
  • nextLine isn't an ArrayList, it's an array. Use a debugger. I bet that there are only two elements in these cases, and you'll need an if statement. – bmargulies Jan 25 '15 at 16:20
  • When you ask about an exception, post the exception stack trace. That should be obvious. – JB Nizet Jan 25 '15 at 16:36
  • Please use `nextLine[2] = "unknown";` and not `nextLine[2] = new String("unknown");` and then read this: http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext-in-java – Tom Jan 25 '15 at 18:02
  • Thanks bmargulies, you were right, so I got round this by creating an if statement for each line and a temporary string[] with the correct number of elements (columns) and was then able to update the missing details before re-copying the temp string back into the Array List. Thanks Tom for highlighting there was no need to instantiate a new string. – Rachle_S Jan 30 '15 at 00:06

2 Answers2

0

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.

Moosa
  • 79
  • 1
  • 2
  • 12
0

Simply check for nextLine.length > 0 (or nextLine.length == EXPECTED_COLUMNS) on every iteration

ancalled
  • 174
  • 6