This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to turn a one-digit into a two-digit one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to turn a one-digit into a two-digit one" Watch "how to turn a one-digit into a two-digit one" New topic
Author

how to turn a one-digit into a two-digit one

Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Hi there !
It's not that dramatic, but I asked myselfe
if there exists a method which turns my
one-digit int (e.g. 1) to a two-digit one (in this case 01) ?
I could write a method by my own.
But I'd like to know if there is something
like that already done ?
That's all
Thanx & bye !
- Thomas -
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What kind of datatype will hold '01'? Well, anything besides char datatypes (String, Object, etc)??
Just curious on how you plan or storing that value or what datatype you are going to store it in.


GenRocket - Experts at Building Test Data
Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Hi Gregg !
I wanna read some Strings out of an ArrayList
and give every String an index from 01 to 15 (or something). That index should be stored into a String.
My code looks something like that:
for (int i=0; i < names.size(); i++) {
Element name = (Element)names.get(i);
out.println(i+". "+name.getFirstName());
}
The output should look like this:
01. Peter
02. Paula
...
10. Jenny
a.s.o.
Problem is, that i is an one-digit int.
O.k. ?
Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
O.k. - You'll probably condemn me now
[ September 11, 2002: Message edited by: Thomas Rochon ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Try creating a custom class for your index, that resembles java.lang.Integer.
Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
Hi Anthony !
Thanx for your reply. I'll try to do so.
I hoped there would be a package java.util.something that contains a method
to do that job for me
Thanx !
- Thomas -
Lance Finney
Ranch Hand

Joined: Apr 26, 2001
Posts: 133
Take a look at java.text.NumberFormat. I think you can get what you want through that.
Be careful about trying to save your integer with the prepended '0'. 01 is not the same to Java as 1. 1 is in base 10. 01 is in octal (base eight). It doesn't really matter for 0-7, but 08 and 09 don't exist, and you could have problems.
 
jQuery in Action, 2nd edition
 
subject: how to turn a one-digit into a two-digit one
 
Similar Threads
sum of two int arrays
Hex Conversion
Protected members
Almost equilateral triangle
Next numbers in series...