Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
import java.io.IOException;
import java.io.StringReader;
import com.opencsv.CSVReader;

public static void main(String[] args) {
		// test of comma separated strings
		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?
Posted
Updated 2-Sep-15 8:43am
v2

1 solution

Quote:
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.

Why do you expect split to take into account something that you don't tell it to care of ?

For your error message, I suggest to look carefully at s1 and s2, you are feeding CSVreader with ill formed srings or at least over complicated strings.
First, you should play with debugger to see where Java choke and why.
Then, you should read the documentation to see what is acceptable as a CSV content.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900