This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes MissingFormatWidthException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "MissingFormatWidthException" Watch "MissingFormatWidthException" New topic
Author

MissingFormatWidthException

J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
Hi ,
I am not able to figure why MissingFormatWidthException is thrown. I used the following code:

import static java.lang.Short.*;
public class MathBoy{
public static void main(String[] args){
long x=12345679;
short y=-22766; //max value of short is 32767
short res=(short)(MAX_VALUE-y);
System.out.printf("%1$,10d %2$(4d,%3$-6d", x, MAX_VALUE - y,res);
System.out.printf("%(d",res);
System.out.println(new Date());
}
}
I get the output: 12,345,679 55533,-10003(10003)Fri Jan 20 19:52:02 IST 2012

if I replace
System.out.printf("%1$,10d %2$(4d,%3$-6d", x, MAX_VALUE - y,res); with
System.out.printf("%1$,d %2$(4d,%3$-6d", x, MAX_VALUE - y,res); //i.e. remove the width specifier for $1
I get the output:
12,345,679 55533,-10003(10003)Fri Jan 20 19:53:36 IST 2012

However, if I replace
System.out.printf("%1$,10d %2$(4d,%3$-6d", x, MAX_VALUE - y,res); with
System.out.printf("%1$,10d %2$(4d,%3$-d", x, MAX_VALUE - y,res); //i.e. remove the width specifier for $3
I get the the following Exception:
Exception in thread "main" java.util.MissingFormatWidthException: -3$d
at java.util.Formatter$FormatSpecifier.checkNumeric(Unknown Source)
at java.util.Formatter$FormatSpecifier.checkInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at MathBoy.main(MathBoy.java:10)

Why is not the Exception thrown in both cases, if at all it is because of the missing format width.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

d and 10d are right-justified.
-d and -6d are left-justified.

It seems there's a default width for right-justified integer values, but not for left-justified.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Jeff Verdegan wrote:d and 10d are right-justified.
-d and -6d are left-justified.

It seems there's a default width for right-justified integer values, but not for left-justified.
I think you are right; look at Formatter and try ctrl-F “MissingFormatWidthException” and you will find it.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:
Jeff Verdegan wrote:d and 10d are right-justified.
-d and -6d are left-justified.

It seems there's a default width for right-justified integer values, but not for left-justified.
I think you are right; look at Formatter and try ctrl-F “MissingFormatWidthException” and you will find it.


Ah, yes, reading the documentation. Who'd ever have thunk it!
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
Thanks, I got it clarified from the java document link.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Jeff Verdegan wrote: . . . Ah, yes, reading the documentation. Who'd ever have thunk it!
It’s well-hidden; you won’t find it unless you know where to look or use ctrl-F.
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
Yeah, the ctrl+f really worked. Thanks once again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MissingFormatWidthException
 
Similar Threads
Illegal format conversion exception for printf
WARNING: Exception getting SSL Cert java.net.SocketException: Socket Closed
Eclipse 3.4.3 Start Problem...ClassNotFoundException:org.eclipse.core.runtime.adaptor.EclipseStarter
Transaction is not being committed
Passed WebLogic 6 certification exam