0

In my program I use for an import export functionality the delimiter ",". The problem for me that format numbers according to the country. For example, the decimal separator for UK is "." and grouping separator is "," and for Germany decimal separator is "," and grouping separator is ".".

So I have a problem during import with files that I export. There is a confusion with delimiter and number format with numbers which contains decimal separator or grouping separator ",".

There is my function during import

public static final String LINE_SEPERATOR = "\n";
public static final String DELIMITER = ",";

protected void writeEntry(final Writer writer, final EntryDTO entry) throws IOException {
    final StringBuilder csvContent = new StringBuilder();
              .append(StringEscapeUtils.escapeCsv(entry.getProduct().getName())).append(DELIMITER)  
              .append(StringEscapeUtils.escapeCsv(entry.getTotalWeight())).append(DELIMITER);
 
    writer.write(csvContent.toString());
}

But during export when I split the line I'm receiving incorrect data due to confusion

final String[] attributes = line.split(delimiter);

Example of data when I import the file : UK Country

Name, Total Weight
example1 ,17,880 (This is grouping separator)

Germany country Name,Total Weight

Name, Total Weight
example2 , 17,88 (This is decimal separator)

I need a generic method that can handle these two cases without modifying the delimiter.

In input when splitting I have 3 values example1, 17, 88. But in my case I want just two values example1 and 17,880

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rodik
  • 271
  • 2
  • 19
  • Does this answer your question? [Java NumberFormat isn't respecting comma as decimal separator](https://stackoverflow.com/questions/21821057/java-numberformat-isnt-respecting-comma-as-decimal-separator) – 9ilsdx 9rvj 0lo Nov 30 '21 at 12:11
  • @9ilsdx9rvj0lo thanks for reply but no, i created the formatters for each country and it works fine. The problem here is Delimiter who has confusion. – Rodik Nov 30 '21 at 12:19
  • 1
    Use a tab `\t` as field delimiter, or quote every field value. – Joop Eggen Nov 30 '21 at 12:37
  • 1
    if you are looking to import a file using delimiters, then it is not uncommon that some of the data itself might validly contain the delimiter. In that case your spec of the file needs to state how to cater for that. For example, you may say that any string that contains a delimiter must be surrounded by double quotes. Then you have the problem of putting a double quote into a string ... etc. For this reason you would usually use a well defined format and a well defined parser (rather than roll your own) that is coded and documented to allow for these situations. – JohnXF Nov 30 '21 at 12:39
  • 1
    Does this answer your question? [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – hfontanez Nov 30 '21 at 12:52
  • 1
    You should probably use a CSV parser, as all well-written ones will handle cases where a comma appears in text (which must then be surrouded by double quotes (`"`)). This is not specific to a comma as decimal separator or thousands separator, but also text containing the delimiter character. – MC Emperor Nov 30 '21 at 13:56
  • This problem was noticed many years ago. No parser can reliably guess which commas are delimiters and which aren’t. This is why the long-standing solution to this, used by nearly all CSV implementations, is to surround values containing the delimiter character in double quotes. And of course, parsers need to recognize double-quoted values and strip the quotes appropriately. – VGR Nov 30 '21 at 16:41

0 Answers0