开发者

csv to array of array of strings

I'm creating a utility class that will make it easier for people to parse a csv string and return an array of array of strings.

My code almost works, but for some reason, when I do a get on my result in my first row, I'm expecting to see 1 row and seeing several rows concatenated on the first row.

Quick example:

a,b,c,d
e,f,g,h

Expecting: {a,b,c,d}, {e,f,g,h}

Result: 开发者_Python百科{a,b,c,d,e,f,g}

public class csvParse
{
protected String                       originalCSV;
protected int                          skipToLine;
protected ArrayList<ArrayList<String>> parsedList;

public ArrayList<ArrayList<String>> getParsedList()
{
    return parsedList;
}

public void setParsedList(ArrayList<ArrayList<String>> parsedList)
{
    this.parsedList = parsedList;
}

public csvParse(String incomingCSV, int skipToLine)
{
    super();
    this.originalCSV = incomingCSV;
    this.skipToLine = skipToLine;
    this.parsedList = new ArrayList<ArrayList<String>>();
    execute();
}

protected void execute()
{
  //  breaking this so there's an error.  read below
    //TODO: Make sure you have data out to X.  May use a try/catch?
    String row;
    String lines[] = this.originalCSV.split("\\n?\\r");

    ArrayList<String> temp = new ArrayList<String>();
    try{
        for (int i = this.skipToLine; i < lines.length; i++)
        {
            row = lines[i];

            //split on commas
            String[] RowData = row.split(",");

            for (int x = 0; x < RowData.length; x++)
            {
                temp.add(RowData[x]);
            }
            this.parsedList.add(temp);
        }
    }
    finally{

    }
}
}


In your execute() method you don't reset the temp variable, so it gets the data from all rows. Just move your initialization within the outer for-loop.


You create "temp" outside the loop that goes through the lines. Thus, you are continually adding fields to the same temp object. You want to move the creation of this inside the loop, so you create a new object for each line.

Also, note that you are not handling embedded commas within a field. Maybe this doesn't occur in your data. But the CSV standard is that a field may be enclosed in quotes, in which case the quotes should be stripped off. If it is enclosed in quotes, it can then contain commas. If a field includes quotes, they should be doubled. For example:

a,"b,c","He said, ""Hello"""

contains three fields:

a
b,c
He said, "Hello"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜