Read Also: [Solved] java.util.IllegalFormatPrecisionException in Java
[Fixed] java.util.MissingFormatArgumentException
Example 1: Producing the exception by using a format specifier which does not have a corresponding argument
We can easily produce this exception by having a format specifier which does not have a corresponding argument as shown below:
public class MissingFormatArgumentExceptionExample {
public static void main(String args[]) {
//First, we will initialize variable str
String str = "100";
/* Below line will throw MissingFormatArgumentException
as we have a format specifier %s that does not have a
corresponding argument */System.out.printf(" %s "+ str);
}
}
Output:
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'
Explanation:
The root cause of this exception is that format specifier %s which does not have a corresponding argument. This exception also occurs if an argument index refers to an argument that does not exist, but that is not the case here.Solution:
The above runtime exception can be resolved by replacing + operator with , as shown below:public class MissingFormatArgumentExceptionExample {
public static void main(String args[]) {
//First, we will initialize variable str with value 100
String str = "100";
/* Below line will not throw MissingFormatArgumentException
as we have replaced + operator with , */System.out.printf(" %s ", str);
}
}
Output:
100
That's all for today. Please mention in the comments in case you are still facing the exception java.util.MissingFormatArgumentException in Java.
0 Commentaires