Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.format

 
Jaap Vermeer
Greenhorn
Posts: 16
  • 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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • 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
  • 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: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic