File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Error with String.format. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error with String.format." Watch "Error with String.format." New topic
Author

Error with String.format.

Alexander McDougall
Greenhorn

Joined: Jan 08, 2013
Posts: 5
Hi there,

I have an error with String.format.

The following is the error that I get:

error: cannot find symbol
String s = String.format("%, d", 1000000000);
symbol: method format(string, int)
location: class String

I have done a search on the error and have not come up with a solution, any help would be appreciated.

Regards
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

What version of Java are you using? The String.format() methods were added in 1.5.


Steve
Alexander McDougall
Greenhorn

Joined: Jan 08, 2013
Posts: 5
Hi,

I am running version 1.7 of Java
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Normally, compiler should auto-box or wrap the primitive argument to an object of Integer type automatically. But in your case, it is not working. I have not tried 1.7, so can not tell. But for the time-being, pass Integer object yourself, like
Its giving you error because the compiler is unable to auto-box or wrap up the primitive to an Integer object automatically.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Rajdeep Biswas wrote: . . . Its giving you error because the compiler is unable to auto-box or wrap up the primitive to an Integer object automatically.
That doesn’t sound right. The argument to the %d tag is an integer, so an int should be acceptable:-
$ java PercentDemo
‍ 1,000,000,000
Note it is using British conventions for the commas and the leading space is there, but I had to add ‍ to the beginning of the line in the quote tags to make it appear. If you used 10000000000, it wouldn’t compile because that number is a long and needs a terminal L.
It says nothing about boxing in the Formatter documentation. It specifically says %d can be applied to an int (link as before, “Conversions” section part 2.1).
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Campbell Ritchie wrote:
Rajdeep Biswas wrote: . . . Its giving you error because the compiler is unable to auto-box or wrap up the primitive to an Integer object automatically.
That doesn’t sound right. The argument to the %d tag is an integer, so an int should be acceptable

Yeah obviously. The method signature the argument is of type Object, so we can expect primitives will work (onyl after auto-boxing, compiler's headache). But OP got an Can not find symbol error that specifies int as the argument. This is unusual, and clearly indicating compiler's failure.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
No, the method signature for format is (String, Object...)
Because the compiler can see that you can box 123 into Integer.valueOf(123), the int 123 is acceptable as a parameter. That does not mean it is actually boxed when the method is executed, only that it can be boxed. You would have to find the JVM code and the code of the Formatter class to verify what actually happens. But that is an implementation detail which you don’t need to know.
In your Java installation folder there is a file called src.zip. Unzip that, go into its java folder, then lang, then the String class, and you find the format method says new Formatter(). So you look in the util folder for the Formatter class and look in its constructor and see what it says.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Alexander McDougall wrote:Hi,

I am running version 1.7 of Java
Try the following instructions at the command line/terminalThe only explanations for your suffering such an error message are
  • 1: You have somehow got an old version of javac
  • 2: You have got a spelling error in the post you quoted
  • 3: You have created your own String class, in which case prefixing the instruction with the package name java.lang. should sort it out.
  • I think no 3 is the most likely.
    Alexander McDougall
    Greenhorn

    Joined: Jan 08, 2013
    Posts: 5
    Hi,

    Thanks for all the help.

    I checked the versions for javac and java, both are Java 1.7.

    I checked through the various steps that you provided. I then checked the classpath. I do have an older version of the java compiler running on my machine. I had to change the classpath to point to the directory I am currently using. There is another directory set up for the older version.

    It is running now without having to make any changes. The autoboxing does work as well. I tried to pass it in as an int, and as an Integer object and both worked.

    Regards

    Alex
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38818
        
      23
    You’re welcome
    Are you sure it is the classpath you changed? You usually change the path to get different versions of Java. You should not usually set a classpath at all; if you created all the classpath yourself, you would probably be better off deleting it (but record it just in case something goes wrong).
    How do you know that passing an int or an Integer to format() represents boxing or unboxing? An Integer, being an Object, has a toString() method and maybe that is being called. Maybe String.valueOf(i) is being called. I don’t know, and I don’t need to know.
    This is how you can verify boxing and unboxing:-But you should not use boxing for arithmetic if at all avoidable; its execution is much slower.
    Are you sure you didn’t have a String class of your own somewhere?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Error with String.format.