Checking the delimiter in a csv file

Hello Experts,
I have some csv files , in some csv files the values are separated by ' ,' and in some other csv files the values are separated by ' ; ' . I need a java code to check whether the csv file is separated by , or ; and if the csv file is separated by ' ; ' then i should be able to  change the separator from ' ; '  to  ' , ' in the csv file.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Please post some (not too much) sample data
I'd do it by checking to see if a ";" exists on every line...

if so, then I'd assume ";" is the delimiter, and you'd have to read the file in line by line and do:

    line = line.replaceAll( ";", "," ) ;

then save it back out again to a new file...
Of course, you may hit problems with data such as:


where a ";" is inside a string var, and "," is the REAL delimiter...

But that can't really be avoided without writing a whole CSV import program...
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

vihar123Author Commented:
First i would like to check the type of the delimiter whether it is , or ; .
try reading the file in line by line...

if every line has a ";" on it, then ";" is probably the delimiter, otherwise it's probably ","

there is no definitive error-proof method that I can think of for automatically doing this...

probably best check for ";", "," and "\t" on every line

String line = "" ;
boolean semicolon = true, comma = true, tab = true ;
while( ( line = bufferedReader.readLine() ) != null )
    if( semicolon && line.indexOf( ";" ) == -1 )
        semicolon = false ;
    if( comma && line.indexOf( "," ) == -1 )
        comma = false ;
    if( tab && line.indexOf( "\t" ) == -1 )
        tab = false ;
if( semicolon && !comma && !tab )
    System.out.println( "Probably ';' is the delimiter" ) ;
else if( !semicolon && comma && !tab )
    System.out.println( "Probably ',' is the delimiter" ) ;
else if( !semicolon && !comma && tab )
    System.out.println( "Probably '\\t' is the delimiter" ) ;
    System.out.println( "Not sure what the delimiter is :-(" ) ;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim CakalicSenior Developer/ArchitectCommented:
Here's a regular expression I found that seems to work. I don't fully like it because it doesn't do the right thing IMO with respect to quotes -- the quotes are returned in the parsed element instead of discarded. But it's close and it is delimiter agnostic for comma and semicolon:

        String regexp = "\\s*[,;]\\s*(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
        Pattern pattern = Pattern.compile(regexp);
        String[] values = pattern.split(line);

vihar123Author Commented:
Hi TimYates ,
Thanks for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.