[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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

0
PearlJamFanatic
Asked:
PearlJamFanatic
  • 6
  • 4
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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:
Tricky! But with some experimenting, the following worked:

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

Open in new window

0
 
CEHJCommented:
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now