Java. lang. IllegalArgumentException: can & #39; t parse arg

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.