MessageFormat is a basic class that is often used in JAVA to customize messages. You can define a message template and use runtime variables to fill the placeholder Place Holder in the template) to achieve flexible output. However, if the newbie is not very careful, the above error message will be easily encountered. Please refer to the following code:
import java.text.MessageFormat;public class TestMessageFormat { public static void main(String[] args) { System.out.println(MessageFormat.format("The username cannot contain any of these characters: (){}",null)); } }
This applet is only used to output a reminder that the user name cannot contain the four symbols () {}, but the above error occurs. The reason is that the braces are used to represent placeholders in MessageFormat, such as {0}, {1 }. It will parse the serial number in the brackets. In the above case, there is no number between the brackets, which leads to an error that cannot be parsed.
The solution is very simple. It is similar to the escape characters in strings. The difference is that single quotation marks are used here). The Code is modified as follows:
import java.text.MessageFormat;public class TestMessageFormat { public static void main(String[] args) { System.out.println(MessageFormat.format("The username cannot contain any of these characters: ()'{'}", null)); }}
Because single quotation marks are also special characters, if you want to display single quotation marks in the output information, you need to put two single quotation marks ~
This article from the "precipitation" blog, please be sure to keep this source http://jupiterbee.blog.51cto.com/3364619/1301261