Home  >  Article  >  Java  >  What issues should be paid attention to when using MessageFormat in Java?

What issues should be paid attention to when using MessageFormat in Java?

WBOY
WBOYforward
2023-04-22 20:31:061143browse

Troubleshooting

After looking for a while, I still had no clue. At this time, I started to panic. I quickly looked at the submission record and confirmed that the code was not written by me, so I felt a little calmer. If others asked, it would be easy to say that it was not written by me! Although I did not write the code, I still have to look at the problem. After all, after you get the money, you have to work.

The next step is to analyze the code. First, we analyzed the most likely error, which is parsing the response logic of the command query. Analyzing the code in this place together with the results obtained by manually executing the command, we found that the response results can be obtained normally based on this response result. After looking at it for a while, I found no problem. Then I looked at the command splicing code. The splicing logic is only one line, so there should be no problem. I had no choice but to look at the log again. This time I found that there was a command to print, send and execute in the log. Taking a closer look, I wonder, why is there an extra comma? He rubbed his glasses again to make sure he saw correctly.

At this time, I will go back and look at the code. There is something wrong with this MessageFormat, but I can't find any evidence for the time being.

public Integer getMaxIndex() {
    // 返回最大的索引值 1035
}
public String queyBySsh() {
    //  这里期望拼接的命令是 show index 1035,但是实际的命令却是 show index 1,035
    // 最终得到的命令多了个英文的逗号 
    String command = MessageFormat("show index {0}", getMaxIndex());
    // 通过SSH执行这个命令查询
}

Then I suddenly remembered a Wiki I had seen before, which seemed to have notes on describing MessageFormat. I quickly went to have a look, and sure enough, it was written that when the parameter of MessageFormat is a numeric type, and when the number exceeds 3 digits, an extra comma will be added for every 3 digits. Therefore, according to the above code logic, we get show index 1,035. There is an extra English comma in the result, and the data cannot be found by manually executing the spliced ​​command. The case is finally solved, spread the flowers, spread the flowers, spread the flowers! ! !

How to solve

The first method is to convert the number into a string and then format it. Modify the above code to the following:

public String queyBySsh() {
    // 这里拼接的时候先调用一下 String.valueOf() 方法
    String command = MessageFormat("show index {0}", String.valueOf(getMaxIndex()));
}

The second method The method is to add FormatStyle of MessageFormat, modify the above code to the following:

public String queyBySsh() {
    // 这里的 # 就是定义的 FamatStyle
    String command = MessageFormat("show index {0, number, #}", getMaxIndex());
}

The above is the detailed content of What issues should be paid attention to when using MessageFormat in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete