Int into String and then back
Please, I need your help...
How to convert Integer into String in a way that the result will be look like this....
Int (5) => String "0005", Int (123) => String "0123" ?
and back operation...
String "0005" => Int "5",String "0123" => Int (123).
Actually, I try to save integer array into a file, and I want that every number will be present by 4 digits. Then, I need to catch these values from a file and convert them back into the integer type.
[510 byte] By [
Neludoffa] at [2007-11-27 5:34:34]

try this example:
import java.text.*;
public class MyNumber
{
public static void main(String[] s)
{
DecimalFormat df = new DecimalFormat("0000");
System.out.println(df.format(15));
}
}
I tried in the following way, but it doesn't work, there is nothing in an output file
import java.io.*;
import java.text.*;
public class Mynumber
{
public static void main(String[] s)
{
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("integer_into_string.txt")));
DecimalFormat df = new DecimalFormat("0000");
for (int i = 0; i<12 ;i++)
out.print(df.format(i) + " ");
}
catch (IOException e) {}
}
}
How to fixed it?
But while we're on the subject of stupidity... catch (IOException e) {}
Never, EVER do that. If the API forces you to catch an exception, at the very least you should print the stack trace: catch (IOException e) {
e.printStackTrace();
}
As you gain more experience you'll get a better feel for dealing with exceptions appropriately, but this is not negotiable: never just swallow exceptions.
(Just kidding about the stupidity, by the way; this too is a common beginner's mistake.)