CSV question: String type converted to Number.
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
enclose quotes around the string, "0000123"
Or use String.format(...):
int n = 763533;
String s = String.format("%09d", n);
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!
>
> 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
> 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.
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");
}
}
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.