Integer to String program

Dear all,

If I would like to change to an integer 2 to a string 02,  how to implement in java program?

In C,  just
         i = 2;
         printf("%02d", i);

How about in java?

Benson
benson031397Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_lychee_Commented:
i think:

int i = 2;
System.out.println(new java.text.DecimalFormat("00").format(i));

not very sure cos i don't have java installed at the mmt...
0
JodCommented:
There are two soltutions.

To do it yourself you would need to make a method like this...

public String makeStringWithLeadingZero(int i) {
  String s = i.toString();
  if (s.length() == 1) s = "0" + s;
  return s;
}

int i = 1;
System.out.println("int i " + i + "turned into String " + makeStringWithLeadingZero(i));


This will turn

55 into String "10" and 1 into String "01"


Or download this...

http://www.efd.lth.se/~d93hb/java/printf/
0
JodCommented:
SOrry lychee - didn't see your comment while writing my answer...
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

_lychee_Commented:
it's alright :>
0
_lychee_Commented:
eh...

i think u should change
i.toString()
to
Integer.toString(i)

or does i.toString() really work?
0
JodCommented:
And my answer is not even right! It should be...

public String makeStringWithLeadingZero(int i) {
  String s = Integer.toString(i);
  if (s.length() == 1) s = "0" + s;
  return s;
}


To be honest,

System.out.println(new java.text.DecimalFormat("00").format(i));

is a neater solution if it works for you benson.

0
JodCommented:
Missed your comment again!

No, i.toString() will never work in a zillion years.

Just wasn't thinking when I wrote the answer...




0
benson031397Author Commented:
lychee has given the answer.

Thanks lychee
0
_lychee_Commented:
should i answer then?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JodCommented:
Even I prefer lychee's answer...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.