Java handle backslash in data

I am reading a csv and a cell has '\'.                               
System.out.println("rowFromAllThreeCsv.get(2)["+k+"]="+rowFromAllThreeCsv.get(2)[k]); \\throws  error java.lang.ArrayIndexOutOfBoundsException: 6

Open in new window

PearlJamFanaticAsked:
Who is Participating?
 
CEHJCommented:
Tricky! But with some experimenting, the following worked:

CSVReader r = new CSVReader(reader, ',', '"', '\0');

Open in new window

0
 
CEHJCommented:
You're addressing a row 'k' that doesn't exists- nothing to do with slashes
0
 
PearlJamFanaticAuthor Commented:
csvReaderList.add(new CSVReader(new FileReader(csvFileNames.get(2))));
while ((nextLineCsv1 = csvReaderList.get(0).readNext()) != null&&(nextLineCsv2= csvReaderList.get(1).readNext()) != null&&(nextLineCsv3 = csvReaderList.get(2).readNext()) != null) {
		    	ArrayList<String[]> rowFromAllThreeCsv=new ArrayList<String[]>();
		    	rowFromAllThreeCsv.add(nextLineCsv1);
		    	rowFromAllThreeCsv.add(nextLineCsv2);
		    	rowFromAllThreeCsv.add(nextLineCsv3);
}

Open in new window


I am using the above to read from the csv. This fails when the data is '\'. I think i need to intiialise CSVReader with the right constructor. Can some one help me with this?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
CEHJCommented:
OK. Can you post the following please

a. an example of a file line you think might be failing
b. the fully-qualified class name of the reader you're using
0
 
PearlJamFanaticAuthor Commented:
Answer to a
25/01/2008 08:46,      datamirror,            50545,      #PGAAGIN,      1020360224,      \,      0,      TRSYS,      CMPLACTS

Answer to b
au.com.bytecode.opencsv.CSVReader


the string[] returned by csvReaderList.get(2).readNext() has only 6 elements with last element having value 1020360224 where as it should have 9.
0
 
PearlJamFanaticAuthor Commented:
Below you can see the exact csv file line. The first one succeeds it has \ followed by space. The next one fails it has only \. When read the first one returns an array of 12 cells and the next one has only 6.

The below works
"25-Jan-2008 08:46:00","datamirror","","50545","#PGAAGIN    ","1020360224","\                             ","0","TRSYS","CMPLACTS    ","",""


The below fails
"25-Jan-2008 08:46:00","datamirror","","50545","#PGAAGIN","1020360224","\","0","TRSYS","CMPLACTS","",""
0
 
PearlJamFanaticAuthor Commented:
I have setup a new question for this. http://www.experts-exchange.com/Programming/Languages/Java/Q_27419994.html  Please post answer there.
0
 
CEHJCommented:
You shouldn't have opened a new question actually. Delete that one please and we'll continue here.
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.