Java MessageFormat - best way to check incomplete format

102 Views Asked by At
String[] params = new String[];
params[0] = "param0";

String pattern = "{2}, {1}, {0}"

String result = MessageFormat.format(pattern, params);

System.out.println(result);

Result:

{2}, {1}, param0

I want to throw an exception if the size of params array does not much the number of placeholders in pattern. What would be the best way to validate this?

MessageFormat format = new MessageFormat(pattern);
if (format.getFormats().length != params.size()) {
  throw new Exception("example");
}
0

There are 0 best solutions below