0

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: {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{

    }
}
}
4
  • 2
    And what's the question? Commented Jun 16, 2011 at 15:31
  • How is incomingCSV generated? Commented Jun 16, 2011 at 15:33
  • @pickypg: I presume the question is, "Why are the actual results not what I expected, as noted with the headings 'expecting' and 'results'?" Commented Jun 16, 2011 at 20:42
  • @Jay You're certainly right, but half the point of this site is to make questions/answers easy to find. Getting the newer users into the habit of doing this makes it easier. :) Commented Jun 16, 2011 at 20:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ugh. How'd I not see that. Thanks.
1

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"

1 Comment

I have code that handles the commas in quotes, I just didnt include it per the requirements of SO to keep it simple. thanks though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.