integer 1 to string => "01"

J

Joshua Jones

I need an easy way to pad integers 1-9 with a zero to form a string
"01", "02", etc., and to leave 10-99 as they are. I looked at
NumberFormat, but it seemed at bit overkill and I'm not sure if it's
the best way (or even if it's possible). Any ideas?
 
B

Bent C Dalager

I need an easy way to pad integers 1-9 with a zero to form a string
"01", "02", etc., and to leave 10-99 as they are. I looked at
NumberFormat, but it seemed at bit overkill and I'm not sure if it's
the best way (or even if it's possible). Any ideas?

Assuming your values are always in the range 1..99:

String pad(int n)
{
if (n <= 9) return "0" + n;
else return String.valueOf(n);
}

If you need to inline it for whatever reason, you might want to use
the conditional operator in stead of if/else.

Cheers
Bent D
 
A

Anand Gopinath

+I need an easy way to pad integers 1-9 with a zero to form a string
+"01", "02", etc., and to leave 10-99 as they are. I looked at
+NumberFormat, but it seemed at bit overkill and I'm not sure if it's
+the best way (or even if it's possible). Any ideas?

Couldn't you just do something like
int i =1;
while( i< 10 )
{
String s = "0" + i;
//Do something with s
}

Anand Gopinath
 
A

Anand Gopinath

+Couldn't you just do something like
+int i =1;
+while( i< 10 )
+{
+ String s = "0" + i;
+ //Do something with s
+}

Oops... you need to increment i in the while loop as well....

int i =1;
while( i< 10 )
{
String s = "0" + i;
//Do something with s
i++;
}
 
R

Roedy Green

I need an easy way to pad integers 1-9 with a zero to form a string
"01", "02", etc., and to leave 10-99 as they are. I looked at
NumberFormat, but it seemed at bit overkill and I'm not sure if it's
the best way (or even if it's possible). Any ideas?


you can pad with 0 by concatentating
"0000000000000000000000".substring( 0, neededZeros).
 
T

Tony Morris

Anand Gopinath said:
+I need an easy way to pad integers 1-9 with a zero to form a string
+"01", "02", etc., and to leave 10-99 as they are. I looked at
+NumberFormat, but it seemed at bit overkill and I'm not sure if it's
+the best way (or even if it's possible). Any ideas?

java.text.NumberFormat

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
M

mromarkhan

Roedy said:

Peace.

Test case:
javac TestingAndQualityAssuranceDepartment.java
Exit code: 0
java TestingAndQualityAssuranceDepartment 01
Exit code: 0


import java.text.DecimalFormat;
class TestingAndQualityAssuranceDepartment
{
public static void main(String [] args)
{
int x=1;
DecimalFormat df = new DecimalFormat("00");
System.out.println(df.format(x));
}
}

Have a good day.
 
R

Roedy Green

Use NumberFormat. It is simple and does the job. Not sure why you
think it is overkill. It is possible to zero pad, although not
zero pad.

The problem is likely wanting code to work with old Javas.
 
L

Liz

Joshua Jones said:
I need an easy way to pad integers 1-9 with a zero to form a string
"01", "02", etc., and to leave 10-99 as they are. I looked at
NumberFormat, but it seemed at bit overkill and I'm not sure if it's
the best way (or even if it's possible). Any ideas?

Here is a working program where the meat is one line.

class test {
public static void main(String args[]) {
int i = 53;
String s=""+"0123456789".charAt(i/10)+"0123456789".charAt(i%10);
System.out.println(s);
}
}


Output is "53"
when i = 3, it prints out "03"
 
G

Gorazd Bozic

Here is a working program where the meat is one line.

class test {
public static void main(String args[]) {
int i = 53;
String s=""+"0123456789".charAt(i/10)+"0123456789".charAt(i%10);
System.out.println(s);
}
}

Alternatively, you could do:

class num {

public static String pad(int number) {
String str = "";
if ((number < 10) && (number >= 0)) str = "0";
str += number;
return str;
}

public static void main(String args[]) {
for (int i = 0; i < 24; i++) System.out.println(pad(i));
}
}

IMO this is more readable. The difference is that Liz's version throws
StringIndexOutOfBoundsException for numbers > 99. This might be what
you want or it might be not.

I would agree with you though that importing large packages and using
general-purpose methods for such simple operations is an overkill.

Gorazd
 
D

Dale King

Hello, Joshua Jones!
You said:
I need an easy way to pad integers 1-9 with a zero to form a string
"01", "02", etc., and to leave 10-99 as they are. I looked at
NumberFormat, but it seemed at bit overkill and I'm not sure if it's
the best way (or even if it's possible). Any ideas?

Use NumberFormat. It is simple and does the job. Not sure why you
think it is overkill. It is possible to zero pad, although not
zero pad.
 
D

Dale King

Hello, Roedy Green !
You said:
The problem is likely wanting code to work with old Javas.

It has been in Java since 1.1. No one should still be trying to
support 1.0.2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top