wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String.format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String.format" Watch "String.format" New topic
Author

String.format

Jaap Vermeer
Greenhorn

Joined: Apr 04, 2006
Posts: 16
Is there a simpler way to write this:

I am trying to convert "3" to "0003", or "32" to "0032" etc.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Yes:

String orderId = String.format("%04s", "3");

Or:

String orderId = String.format("%04d", 3);

Why all the hassle with an array??
[ April 27, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jaap Vermeer
Greenhorn

Joined: Apr 04, 2006
Posts: 16
Because my compiler sais:
The method format(String, Object[]) in the type String is not applicable for the arguments (String, int)

I'm using Java 1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Originally posted by Jaap Vermeer:
Because my compiler sais:
The method format(String, Object[]) in the type String is not applicable for the arguments (String, int)

I'm using Java 1.4


I don't think the format method -- in either form -- is available in Java 1.4. It was added in Java 5.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.format
 
Similar Threads
Questions regarding the article "String Literally" written by Corey McGlone
String.format() - can i pass array ?
String.format questions
double
stream output variables to specific positions in output file