Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.k. - You'll probably condemn me now
[ September 11, 2002: Message edited by: Thomas Rochon ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try creating a custom class for your index, that resembles java.lang.Integer.
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic