【问题标题】:Message format choice in properties file属性文件中的消息格式选择
【发布时间】:2014-04-09 11:11:57
【问题描述】:

我看过这个link,看不出我做错了什么。我正在使用 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."

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

【问题讨论】:

    标签: java properties messageformat


    【解决方案1】:

    我希望,下面的 sn-p 可以解决这个问题。

    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 次尝试。

    【讨论】:

    • 谢谢。不知道为什么我没有尝试过,但这让我走上了正确的道路。事实证明,如果消息格式是字符串表示形式,它不会将其解析为数字!只需将我传递的内容更改为对象数组
    猜你喜欢
    • 2012-08-21
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    相关资源
    最近更新 更多