2

Using jcsv I'm trying to parse a CSV to a specified type. When I parse it, it says length of the data param is 1. This is incorrect. I tried removing line breaks, but it still says 1. Am I just missing something in plain sight?

This is my input string csvString variable

"Symbol","Last","Chg(%)","Vol",
INTC,23.90,1.06,28419200,
GE,26.83,0.19,22707700,
PFE,31.88,-0.03,17036200,
MRK,49.83,0.50,11565500,
T,35.41,0.37,11471300,

This is the Parser

  public class BuySignalParser implements CSVEntryParser<BuySignal> {
  @Override
  public BuySignal parseEntry(String... data) {

    // console says "Length 1"
    System.out.println("Length " + data.length);

    if (data.length != 4) {
      throw new IllegalArgumentException("data is not a valid BuySignal record");
    }

    String symbol = data[0];
    double last = Double.parseDouble(data[1]);
    double change = Double.parseDouble(data[2]);
    double volume = Double.parseDouble(data[3]);

    return new BuySignal(symbol, last, change, volume);
  }
}

And this is where I use the parser (right from the example)

CSVReader<BuySignal> cReader = new CSVReaderBuilder<BuySignal>(new StringReader( csvString)).entryParser(new BuySignalParser()).build();
List<BuySignal> signals = cReader.readAll();
FlavorScape
  • 13,301
  • 12
  • 75
  • 117
  • 1
    Is it saying that the length is 5? Include the actual length value in the exception. You have 4 commas in each line, and that might mean 5 values.... – AgilePro Nov 30 '13 at 23:52
  • 1
    jcsv allows you to change the delimit character. The example in documentation is semicolon. Are you sure it is set to use comma? – AgilePro Nov 30 '13 at 23:54
  • Ah, CSVStrategy.UK_DEFAULT so funny, I've worked with CSV in other langs quite a while, never encountered semi-colons as a separator. – FlavorScape Nov 30 '13 at 23:57
  • Here is the CSV parse that I use: http://agiletribe.wordpress.com/2012/11/23/the-only-class-you-need-for-csv-files/ – AgilePro Nov 30 '13 at 23:58
  • seems like the problem is twofold: the delimiter was incorrect, and the trailing comma is not to spec (as provided by the external source)? So in my initial stringbuilder I will have to eliminate the trailing. Once I change to use commas, says length is 5 =) – FlavorScape Dec 01 '13 at 00:01

2 Answers2

1

You shouldn't have commas at the end of lines. Generally there are cell delimiters (commas) and line delimiters (newlines). By placing commas at the end of the line it looks like the entire file is one long line.

disrvptor
  • 1,592
  • 12
  • 23
1

jcsv allows different delimiter characters. The default is semicolon. Use CSVStrategy.UK_DEFAULT to get to use commas.

Also, you have four commas, and that usually indicates five values. You might want to remove the delimiters off the end.

I don't know how to make jcsv ignore the first line

I typically use CSVHelper to parse CSV files, and while jcsv seems pretty good, here is how you would do it with CVSHelper:

Reader reader = new InputStreamReader(new FileInputStream("persons.csv"), "UTF-8");

//bring in the first line with the headers if you want them
List<String> firstRow = CSVHelper.parseLine(reader);

List<String> dataRow = CSVHelper.parseLine(reader);
while (dataRow!=null) {

    ...put your code here to construct your objects from the strings

    dataRow = CSVHelper.parseLine(reader);
}
AgilePro
  • 5,588
  • 4
  • 33
  • 56
  • So I did both fixes... Now I see that it is trying to include the column definitions. Is there a way to ignore this while parsing, or do I just have to do this manually while reading in the data? I will update question. Guess .NET has really spoilt me =) – FlavorScape Dec 01 '13 at 00:06
  • Well, like I said, I use [The Only CSV Parser you need](http://agiletribe.wordpress.com/2012/11/23/the-only-class-you-need-for-csv-files/) for parsing CSV, because you write your own loop, and in that case you just skip the first entry. jcsv does not have anything obvious for skipping the first row, however, it seems like it surely must handle this case. – AgilePro Dec 01 '13 at 00:10