Nicole
Nicole

Reputation: 1285

Java IO Exception when using Weka CSVLoader in Java code

I am trying to automate some Weka classification using Java code. I'm getting the following error when I try to read my csv file:

java.io.IOException: wrong number of values. Read 98, expected 97, read Token[EOL], line 3 at weka.core.converters.ConverterUtils.errms(ConverterUtils.java:912) at weka.core.converters.CSVLoader.getInstance(CSVLoader.java:747) at weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:570) at Classifier_Search.readFile(Classifier_Search.java:18) at Classifier_Search.main(Classifier_Search.java:178) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

I get the same error when I try to feed the csv file in through the Weka GUI. As far as I know, the csv file is no different from other csv files that I've successfully used with Weka. Any thoughts about what could be causing this error?

I haven't been able to find a satisfactory answer on OldNabble for this - I've tried the one suggestion that I managed to find and it did not work. The one suggestion was to encapsulate the entries in double quotes - that did not change anything.

Just for clarity (and because I don't know how to upload things), the csv files look like this:

C1V1, C2V1, C3V1, ... Class,

4000.5, 3890.6, 2900.7, ... N,

...

...

There are 97 entries per row. In another version, I have double quotes around the strings and chars (in the first row and last column) and, as I previously stated, that doesn't help.

Thanks!

Upvotes: 2

Views: 8582

Answers (2)

Du-Lacoste
Du-Lacoste

Reputation: 12757

I also had the same problem when using weka tool. I was able to sort it out like this.

enter image description here

When you get this type exception. Locate the line which is above the prompted line. Eg: In my case I was prompted that exception is due to some fault in the line 80542 so I need to check the 80541 line. Then you can see there is an extra comma or extra double quotes in the line. Remove that extra comma or double quotes and save the file. Run it again. Then it works properly without any exceptions.

Upvotes: 1

Nicole
Nicole

Reputation: 1285

I've discovered the difference: if there is a comma at the end of a line in a .csv file, it causes csvloader to throw this error. So, if you're trying to use this, eliminate the commas at the ends of lines in your .csv file. I don't know why only line 3 and not lines 1 and 2 threw the error (all lines were identical), but this fixed it.

Upvotes: 0

Related Questions