简体   繁体   English

使用 MaskFormatter 格式化字符串会引发“无效字符”异常

[英]Format string with MaskFormatter throws “Invalid character” exception

I have a function in which I need to set a string to the following format: #####-####-## and padded with leading zeroes.我有一个 function ,我需要将字符串设置为以下格式:#####-####-## 并用前导零填充。

eg:例如:

1234567 will become 00001-2345-67 1234567将变为00001-2345-67

98765432109 will become 98765-4321-09 98765432109将变为98765-4321-09

I'm using MaskFormatter and my impression from the documentation was that A represented any letter or digit, and that setPlaceHolderCharacter would pad the string.我正在使用MaskFormatter ,我对文档的印象是A代表任何字母或数字,而setPlaceHolderCharacter将填充字符串。

I know technically it's for use with text fields but had hoped I could use it in a back-end function, if not is there a different component i should be using?我知道从技术上讲它是用于文本字段的,但我希望我可以在后端 function 中使用它,如果没有,我应该使用不同的组件吗?

I have this code below but it throws an exception java.text.ParseException: Invalid character: 1 when it hits mask.valueToString(ndc)我在下面有这段代码,但它抛出异常java.text.ParseException: Invalid character: 1 when it hit mask.valueToString(ndc)

public class NdcFormatter implements MapFunction<Purchase, Purchase> {

    private final String maskFormat = "AAAAA-AAAA-AA";
    private final Character placeholder = '0';


    @Override
    public Purchase map(Purchase purchase) throws ParseException {
        if(purchase != null){

            String ndc = purchase.getNdc();
            MaskFormatter mask = new MaskFormatter(maskFormat);
            mask.setPlaceholderCharacter(placeholder);
            String result = mask.valueToString(ndc);

            purchase.setNdc(result);

        }

        return purchase;
    }
}

Here is how to fix ERROR java.text.ParseException: Invalid character: ... in MaskFormatter以下是如何修复错误java.text.ParseException: Invalid character: ... in MaskFormatter

Solution the error is in valueToString method, you have to add this line of code:解决方法错误在valueToString方法中,你必须添加这行代码:

mask.setValueContainsLiteralCharacters(false);

but you will get a problem like that:但你会遇到这样的问题:

  • when you set 1234567 it will return you 12345-6700-00当您设置1234567时,它将返回您12345-6700-00
  • to get 00001-2345-67 you have to pass 00001234567要获得00001-2345-67 ,您必须通过00001234567

so you have to fix your input value before formatting.所以你必须在格式化之前修复你的输入值。

here is the complete code:这是完整的代码:

public class NdcFormatter implements MapFunction<Purchase, Purchase> {

    private final String maskFormat = "AAAAA-AAAA-AA";
    private final Character placeholder = '0';


    @Override
    public Purchase map(Purchase purchase) throws ParseException {
        if(purchase != null){

            String ndc = purchase.getNdc();
            MaskFormatter mask = new MaskFormatter(maskFormat);
            mask.setPlaceholderCharacter(placeholder);
            mask.setValueContainsLiteralCharacters(false);
            String result = mask.valueToString(ndc);

            purchase.setNdc(result);

        }

        return purchase;
    }
}

to pad the leading zeroes by yourself here is a simple code example.自己填充前导零是一个简单的代码示例。

    if(ndc.length() < maskFormat.length()) {
        String pad = "0";
        for(int i = 0; i < maskFormat.length() -ndc.length() - 3; i++ ) {
            pad = pad + "0" ;
        }
        ndc = pad + ndc;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM