Also, this exception occurs when your val is not an integer, in the question case this is not an issue, but just answering for other people's help.
String val = "100"; String.format("%3d", val);
To resolve make sure you parse String to int.