8 Replies Latest reply: Jul 12, 2007 8:04 PM by camickr RSS

    CSV question:  String type converted to Number.

    800340
      hi there,
      quick question: I have a value in one of my objects stored as type String.
      when I output the value to a CSV file it works fine unless the value is a number that starts with a 0.

      eg: 000763533
      the output in the cell is 763533. this is caused obviously because the cell regards the value as a number and not string.

      how can I circumvent this and have the output 'as is' 000763533?

      thanks
        • 1. Re: CSV question:  String type converted to Number.
          jwenting
          NumberFormat.
          • 2. Re: CSV question:  String type converted to Number.
            camickr
            enclose quotes around the string, "0000123"
            • 3. Re: CSV question:  String type converted to Number.
              800282
              Or use String.format(...):
              int n = 763533;
              String s = String.format("%09d", n);
              • 4. Re: CSV question:  String type converted to Number.
                800340
                mmm....unfortunately, none of the solutions is working.
                FileWriter writeCSV = new FileWriter(fileName);
                PrintWriter fileOutput = new PrintWriter(writeCSV);     
                .
                .
                .
                String i  = "000033";
                i="\""+i+"\"";
                fileOutput.println(i) // output is 33
                fileOutput.println("0000550") // output is 550
                as per:
                int n = 763533;
                String s = String.format("%09d", n);
                error message:
                the method format(String, Object[]) in the type String is not applicable for arguments (String, int)


                any idea?

                thank you!
                • 5. Re: CSV question:  String type converted to Number.
                  807605
                  >
                  any idea?

                  thank you!
                  yep, upgrade to java 6 (5 may also work here; I'm not sure)

                  Addendum: API check confirms that java 5 (1.5) works.

                  Message was edited by:
                  petes1234
                  • 6. Re: CSV question:  String type converted to Number.
                    camickr
                    mmm....unfortunately, none of the solutions is working.
                    Why do people say something doesn't work without posting the 5 line program they use to test the suggestions?

                    Of course it works!

                    If it doesn't then you have some wierd version of Java installed or you are working on some wierd platform.
                    • 7. Re: CSV question:  String type converted to Number.
                      800340
                      let the record reflect your honor...
                      import java.io.FileWriter;
                      import java.io.IOException;
                      import java.io.PrintWriter;
                      
                      public class CSVWriter
                      {
                           private final static String path="C:\\Documents and Settings\\xianwinwin\\Desktop\\abc"; 
                           
                           public static void main(String args[]) throws IOException
                           {
                                String fileName = "test.csv";
                                FileWriter writeCSV = new FileWriter(path+"/"+fileName);
                                PrintWriter fileOutput = new PrintWriter(writeCSV);     
                                
                                String i  = "000033";
                                i="\""+i+"\"";
                                fileOutput.println(i); // output is 33
                                fileOutput.println("0000550"); // output is 550
                                
                                fileOutput.close();          
                                
                                System.out.println("go to directory and open file");
                           }
                           
                      }
                      • 8. Re: CSV question:  String type converted to Number.
                        camickr
                        Much better SSCCE. Now we don't have to guess if you made any silly mistakes.

                        The only comment I have is don't use such a complicated path. Don't assume people that help you on the forum use Windows. There is no need to specify a fancy path. Just specify the filename and the file will be created in the same directory as your class. When I ran you code I made the following change:
                        FileWriter writeCSV = new FileWriter(fileName);
                        And here is my output:
                        go to directory and open file
                        
                        c:\java\temp>type test.csv
                        "000033"
                        0000550
                        I'm using JDK1.4.2 on XP. I have no idea why is doesn't work for you. As I said in my previous posting, its a "wierd" problem.