Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

CSV output issue from database input

I have requirement where I am getting data from database data and sending it to CSV file into multiple columns accordingly.

One of the column data coming as

abc,corp
def,inc

etc.

Due to the comma in the data in CSV instead of
'abc,corp' being sent to single column cell it is sending it two separate adjacent cells of two columns. Due to this all other column data that comes after that is being pushed as well.

I am trying to solve this issue for many days but no luck yet.  I cannot use any CSV Libraries etc.

Is there is simple proof of concept or working example which i can try and later solve my problem.
please advise
Any links resources ideas highly appreciated. Thanks in advance
SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I cannot use any CSV Libraries etc.
Why not? The alternative is what - to use potentially buggy home-brewed code? Parsing CSV is not as easy as it looks at first glance - as you're already seeing
Avatar of gudii9

ASKER

can you send me some sample code/ link in which takes a simple string let us say
abc,def
and then that java program replaces the comma with a blank space so that i can see out put in the console as
abc def

I will try to extend on top of that. please advise
Well that could be as simple as

s = s.replace(",", " ");

Open in new window

Avatar of gudii9

ASKER

the challenge is the data base input values i am not sure which position the comma(,) has. That should be still fine right
It doesn't matter what the position is
Avatar of gudii9

ASKER

any simple example link illustrating this scenario. Please advise
If you want to try to 'roll your own' then try

if (s.contains(",")) {
   s = String.format("\"%s\"", s);
}

Open in new window

Avatar of gudii9

ASKER

I see here CSV to mysql database not other way round. I am still searching to find example from database to CSV. Please advise
I'm not sure what the problem is of selecting values and appending commas ...
Avatar of gudii9

ASKER

some one fixed it already as below i guess

      StringBuilder builder = new StringBuilder();
            builder.append("\"\u00A0")                        
                        .append(getVendorName()).append("\",\"").append(", ")
                        .append("]");;

Not sure what above code means.what is u00A0 and what is  meaning of
.append(getVendorName()).append("\",\"").append(", ")


Now below kind of data coming into one cell of the spreadsheet only  as attached, even though  there is a comma.



abc,corp

Can you please advise how they are able to bring above data to one cell even there is comma. How they escaped comma from incoming data.

I tried looking below site
http://stackoverflow.com/questions/8501072/string-unicode-remove-char-from-the-string

but it is still not clear to me.

what is meaning of below regex
fax = fax.replaceAll("\\D", "");
            return fax.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "$1-$2-$3");
please advise.

i tried to still go ahead and replace comma with space but did not work.


My code is as below which is appending each and every column

      StringBuilder builder = new StringBuilder();
            builder.append("\"\u00A0")                        
                        .append(getVendorName()).append("\",\"").append(", ")
                        .append(getVendorID()).append("\",\"").append(", ")
                          .append(getVendorFax()).append("\",\"").append(", ")
                        .append("]");;

Open in new window


Now i need to apply the condition you mentioned
if (s.contains(",")) {
   s = String.format("\"%s\"", s);
}

I tried as below but not workinng
      StringBuilder builder = new StringBuilder();
            builder.append("\"\u00A0")                        
                        .append(getVendorName()).contains(",").format("\"%s\"", s).append("\",\"").append(", ")
                        .append(getVendorID()).append("\",\"").append(", ")
                          .append(getVendorFax()).append("\",\"").append(", ")
                        .append("]");;

Open in new window



Getting 's' is unknown variable. please advise
VendorSpreadsheetSS.jpg
Avatar of gudii9

ASKER

I change as below by calling newly created separate method


 StringBuilder builder = new StringBuilder();
            builder.append("\"\u00A0")                        
                        .append(formatVendorName(getVendorName())).append("\",\"\u00A0")
                        .append(getVendorID()).append("\",\"").append(", ")
                          .append(getVendorFax()).append("\",\"").append(", ")
                        .append("]");;

Open in new window


I created separate new method
public static String formatVendorName(String vendorName) {
            if (vendorName.contains(",")) {
                  vendorName= String.format("\"%s\"", vendorName);
                  }
            return vendorName;
      }

The problem i faced is it removed the comma but it pushed 'def' part to next cell with "" as attached and pushing off the phone number also to next cell. please advise how to fix it
VendorSpreadsheetSS2.jpg
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

>>\u00A0 is just a non-breaking space

what is the meaning of non breaking space. what is the link where i can see all these different characters.

so use as below
 StringBuilder builder = new StringBuilder();
            builder.append("\"\u00A0")                        
                        .append(formatVendorName(getVendorName())).append("\",\"").append(", ")
                        .append(getVendorID()).append("\",\"").append(", ")
                          .append(getVendorFax()).append("\",\"").append(", ")
                        .append("]");;

Open in new window

:)