Hi guys,
I'm converting a project from C# to Java. I have to analyze some
CSV files. Everything works fine in C#, but in Java I can't make it work the right
way.
For example, there are two lines in the file:
"US", "Dimanco, Inc", "001"
"US", "Class "B"", "002"
I'm trying to split these lines into separate items using comma as a separator.
Here's my code:
import java.io.IOException;
import java.io.StringReader;
import com.opencsv.CSVReader;
public static void main(String[] args) {
String[] c1 = null;
String[] c2 = null;
String s1 = "\"US\",\"Dimanco, Inc\", \"001\"";
String s2 = "\"US\", \"Class\"B\"\", \"002\"";
c1 = s1.split(",", -1);
c2 = s2.split(",", -1);
System.out.println(s1);
System.out.println(s2);
System.out.println("c1.length = " + c1.length);
printArray(c1);
System.out.println("c2.length = " + c2.length);
printArray(c2);
CSVReader reader1 = new CSVReader(new StringReader(s1));
String[] tokens1 = null;
try {
while((tokens1 = reader1.readNext())!=null) {
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("tokens1.length = " + tokens1.length);
printArray(tokens1);
CSVReader reader2 = new CSVReader(new StringReader(s2));
String[] tokens2 = null;
try {
while((tokens2 = reader2.readNext())!=null) {
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("tokens2.length = " + tokens2.length);
printArray(tokens2);
}
static void printArray(String[] arr) {
String out = new String() ;
for(int i=0; i< arr.length; i++) {
out += arr[i] + " <-->";
}
System.out.println(out);
}
First I try to use common split() function. The result is:
"US","Dimanco, Inc", "001"
"US", "Class"B"", "002"
c1.length = 4
"US" <-->"Dimanco <--> Inc" <--> "001" <-->
c2.length = 3
"US" <--> "Class"B"" <--> "002" <-->
The first line split into 4 items and the second line split into 3. Obviously
split() does not ignore comma inside double quotes. I'd like each line to split
into 3 items.
Second I try OpenCSV with this result:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at com.opencsv.CSVParser.isAllWhiteSpace(CSVParser.java:543)
at com.opencsv.CSVParser.parseLine(CSVParser.java:372)
at com.opencsv.CSVParser.parseLineMulti(CSVParser.java:299)
at com.opencsv.CSVReader.readNext(CSVReader.java:276)
at PortiaMoxy.StringSplitTest.main(StringSplitTest.java:43)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
Does anyone know where I am wrong?