Ethan Ethan - 7 months ago 13
Java Question

How could i turn this into a loop?

public String toString()
{
if(zipCode < 10000)
{
if(zipCode < 1000)
{
if(zipCode < 100)
{
if(zipCode < 10)
{
return "0" + "0" + "0" + "0" + Integer.toString(zipCode);
}
return "0" + "0" + "0" + Integer.toString(zipCode);
}
return "0" + "0" + Integer.toString(zipCode);
}
return "0" + Integer.toString(zipCode);
}
else
{
return Integer.toString(zipCode);
}
}


Is there a loop that i could do that would add a zero in front of the string depending on what exponent of 10 the number stored in zipCode is? I'm not looking to get rid of the zeroes I'm looking to keep the zeroes, but the zeroes are taken away.

Answer
String.format("%05d", number);

It's implemented yet.