File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.format

 
Jaap Vermeer
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jaap Vermeer
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic