Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MissingFormatWidthException

 
J C upadhyay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got it clarified from the java document link.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the ctrl+f really worked. Thanks once again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic