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

huangapple go评论100阅读模式
英文:

Format string with MaskFormatter throws "Invalid character" exception

问题

我有一个函数,我需要将字符串设置为以下格式:#####-####-##,并用前导零填充。

例如:

`1234567` 将变成 `00001-2345-67`

`98765432109` 将变成 `98765-4321-09`

我正在使用 [MaskFormatter][1],从文档中的印象是 `A` 代表任何字母或数字,而 `setPlaceHolderCharacter` 会填充字符串。

我知道从技术上讲,它是用于文本字段的,但希望我可以在后端函数中使用它,如果不能,是否应该使用不同的组件?

我有下面的代码,但当它执行 `mask.valueToString(ndc)` 时,会抛出异常 `java.text.ParseException: Invalid character: 1`。

public class NdcFormatter implements MapFunction<Purchase, Purchase> {

private final String maskFormat = &quot;AAAAA-AAAA-AA&quot;;
private final Character placeholder = &#39;0&#39;;


@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;
}

}


[1]: https://docs.oracle.com/javase/7/docs/api/javax/swing/text/MaskFormatter.html
英文:

I have a function in which I need to set a string to the following format : #####-####-## and padded with leading zeroes.

eg:

1234567 will become 00001-2345-67

98765432109 will become 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.

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?

I have this code below but it throws an exception java.text.ParseException: Invalid character: 1 when it hits mask.valueToString(ndc)

public class NdcFormatter implements MapFunction&lt;Purchase, Purchase&gt; {

    private final String maskFormat = &quot;AAAAA-AAAA-AA&quot;;
    private final Character placeholder = &#39;0&#39;;


    @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;
    }
}

答案1

得分: 3

以下是关于如何修复MaskFormatter中的ERROR java.text.ParseException: Invalid character: ...的部分翻译:

解决方案:错误出现在valueToString方法中,您需要添加以下代码行:

mask.setValueContainsLiteralCharacters(false);

但您可能会遇到一个问题:

  • 当您设置1234567时,它将返回12345-6700-00
  • 要获得00001-2345-67,您需要传递00001234567

因此,在格式化之前,您需要修复输入值。

以下是完整的代码示例:

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;
    }
}

如果您想自己填充前导零,以下是一个简单的代码示例:

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;
}

请注意,我只翻译了代码部分,没有包含其他内容。

英文:

Here is how to fix ERROR java.text.ParseException: Invalid character: ... in MaskFormatter

Solution the error is in valueToString method, you have to add this line of code :

mask.setValueContainsLiteralCharacters(false);

but you will get a problem like that :

  • when you set 1234567 it will return you 12345-6700-00
  • to get 00001-2345-67 you have to pass 00001234567

so you have to fix your input value before formatting .

here is the complete code :

public class NdcFormatter implements MapFunction&lt;Purchase, Purchase&gt; {

    private final String maskFormat = &quot;AAAAA-AAAA-AA&quot;;
    private final Character placeholder = &#39;0&#39;;


    @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() &lt; maskFormat.length()) {
		String pad = &quot;0&quot;;
		for(int i = 0; i &lt; maskFormat.length() -ndc.length() - 3; i++ ) {
			pad = pad + &quot;0&quot; ;
		}
		ndc = pad + ndc;
	}

huangapple
  • 本文由 发表于 2020年8月11日 19:02:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63356840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定