Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Checking the delimiter in a csv file

Posted on 2005-03-30
8
Medium Priority
?
282 Views
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.

 Thanks............
0
Comment
Question by:vihar123
8 Comments
 
LVL 86

Expert Comment

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

Expert Comment

by:TimYates
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...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13661324
Of course, you may hit problems with data such as:

  1,"tim;yates",2

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...
0
Technology Partners: 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!

 

Author Comment

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

Accepted Solution

by:
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" ) ;
else
    System.out.println( "Not sure what the delimiter is :-(" ) ;
0
 
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);

Regards,
Jim
0
 

Author Comment

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

Expert Comment

by:TimYates
ID: 13866794
:-)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

577 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