0

我看过这个链接,看不出我做错了什么。我正在使用 MessageFormat 从文件中获取属性,并希望根据传入的整数是 1 还是更高来使属性动态化。文件中的属性如下所示:

Prop1=Invalid password entered. You have {0,number,integer} {0,choice,1#attempt|1<attempts} remaining.

在代码中,我读入了这个属性,然后将一个参数数组传递给MessageFormat.format()(我传递了一个数组以保证所有属性的一致性。在这种情况下,唯一存在的对象是整数值的字符串表示形式)同时调试代码,我可以看到这个字符串被读取,但MessageFormat.format()似乎没有按预期格式化字符串。结果如下所示:

"Invalid password entered. You have {0,number,integer} {0,choice, 1#attempt|1<attempts} remaining."

谁能告诉我我做错了什么?如前所述,我传递了整数值的字符串表示形式,但我假设上面的代码能够处理它。我的理解有误吗?

4

1 回答 1

3

我希望,下面的片段可以解决这个问题。

String msg  = "Invalid password entered. You have {0,number,integer} {0,choice,   1#attempt|1<attempts} remaining.";
int attemptCount = 1; 
System.out.println(MessageFormat.format(msg, attemptCount, attemptCount));

结果是,

输入的密码无效。您还剩 1 次尝试。

于 2014-04-09T11:35:10.950 回答