Checking the delimiter in a csv file

Posted on 2005-03-30
Medium Priority
Last Modified: 2010-03-31
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.

Question by:vihar123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 13661276
Please post some (not too much) sample data
LVL 35

Expert Comment

ID: 13661317
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...
LVL 35

Expert Comment

ID: 13661324
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...
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more


Author Comment

ID: 13661424
First i would like to check the type of the delimiter whether it is , or ; .
LVL 35

Accepted Solution

TimYates earned 1000 total points
ID: 13661474
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 :-(" ) ;
LVL 19

Expert Comment

by:Jim Cakalic
ID: 13663557
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);


Author Comment

ID: 13866771
Hi TimYates ,
Thanks for your help.
LVL 35

Expert Comment

ID: 13866794

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question