Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “接收”;不是有效的字符构造函数输入;传递小数时出错_Java_Spring_Bigdecimal - Fatal编程技术网

Java “接收”;不是有效的字符构造函数输入;传递小数时出错

Java “接收”;不是有效的字符构造函数输入;传递小数时出错,java,spring,bigdecimal,Java,Spring,Bigdecimal,当传递十进制时,我接收到“不是有效的字符构造函数输入”。我的代码如下。我研究发现,通过从BigDecimal更改为Formatdeciaml可以解决这个问题,但我不能这样做,因为这样我的映射器也需要更改。任何帮助都将不胜感激 控制器 String postagePaid = (String) request.getParameter("tPostagePaid"); String insuranceFees = (String) request.getPara

当传递十进制时,我接收到“不是有效的字符构造函数输入”。我的代码如下。我研究发现,通过从BigDecimal更改为Formatdeciaml可以解决这个问题,但我不能这样做,因为这样我的映射器也需要更改。任何帮助都将不胜感激

控制器

  String postagePaid = (String) request.getParameter("tPostagePaid");
                String insuranceFees = (String) request.getParameter("tInsuranceFees");
                String registeredFees = (String) request.getParameter("tRegisteredFees");
                String codFees = (String) request.getParameter("tCODFees");
                String insRegisteredCODFees = (String) request.getParameter("tInsuranceFees");
                System.out.println("insurance Fee: " + insuranceFees);
                if (postagePaid != null && !insuranceFees.isEmpty()) { // postage paid amount
                    claim.setClPostagePaidAmt(new BigDecimal(postagePaid));
                }
                if (insuranceFees != null && !insuranceFees.isEmpty()) { // Insurance Fees
                    claim.setClInsuranceFee(new BigDecimal(insuranceFees));
                }
                if (registeredFees != null && !insuranceFees.isEmpty()) { // Registered Fees
                    claim.setClRegisteredFee(new BigDecimal(registeredFees));
                }
                if (codFees != null && !insuranceFees.isEmpty()) { // COD Fees
                    claim.setClCodFee(new BigDecimal(codFees));
                }
                claim.setClInsRegCodAmt(null);
错误:

Caused by: java.lang.NumberFormatException: Not a valid char constructor input: 
at java.math.BigDecimal.bad(BigDecimal.java:1859)
at java.math.BigDecimal.charParser(BigDecimal.java:1303)
at java.math.BigDecimal.<init>(BigDecimal.java:922)
at java.math.BigDecimal.<init>(BigDecimal.java:901)
at   gov.usps.oic.controller.OicClaimEntryControlloer.submitClaim(OicClaimEntryControlloer.java:991)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
原因:java.lang.NumberFormatException:不是有效的字符构造函数输入:
在java.math.BigDecimal.bad(BigDecimal.java:1859)
位于java.math.BigDecimal.charParser(BigDecimal.java:1303)
在java.math.BigDecimal.(BigDecimal.java:922)
在java.math.BigDecimal.(BigDecimal.java:901)
位于gov.usps.oic.controller.oicclaimentrycontroller.submitClaim(oicclaimentrycontroller.java:991)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)中
位于java.lang.reflect.Method.invoke(Method.java:611)
位于org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
位于org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
位于org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
位于org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
位于org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
位于org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
位于org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)

根据您的评论,这个问题最终是关于获取未知输入并将其解析为
BigDecimal
。您可以使用正则表达式从
字符串中获取数值

public class Program {  
    private static Object lock = new Object();
    private static final Pattern BIG_DECIMAL_MATCHER;

    static {
        synchronized (lock) {
            BIG_DECIMAL_MATCHER = Pattern.compile("-?(?:\\d+(?:\\.\\d+)?|\\.\\d+)"); 
            // credit to stackoverflow.com/questions/14343551
        }
    }

    /**
     * Attempts to force a given String value to a valid BigDecimal. If there are 
     * multiple valid BigDecimal values, only the first is returned.
     */
    static BigDecimal forceBigDecimal(String value) { 
        Matcher moneyMatcher = BIG_DECIMAL_MATCHER.matcher(value);

        while (moneyMatcher.find()) {
            return new BigDecimal(moneyMatcher.group());
        }
        throw new NumberFormatException(
            String.format("Valid number not found in value: %s", value));
    }
}
用法示例:

List<String> moneyValues = new ArrayList<String>() {{
    add("$42.35"); add("Fee: $10.25"); add("฿1200");
    add("obviously not valid input"); 
    add("not too 42 sure . 12about this one"); 
    add("or this $..3"); 
    add("$42.35 $84.50");
}};

for (String value : moneyValues) {
    try {   
        BigDecimal parsed = Program.forceBigDecimal(value); 
        System.out.println(parsed);
    } catch (NumberFormatException ex) { 
        ex.printStackTrace();
        continue;
    }
}

你确定要通过指定的语法传递解析的字符串吗?你能发布导致异常的输入字符串吗?我刚刚重新编译了前面的代码,将$g$添加到我的输入字符串中,传递字符串是$1.50。这将很快结束你的Bigdecimal解析梦想;),考虑一下你自己的问题,它可能对将来有用。visitors@jdphenix您知道如何在更新DB之前从字符串中提取$。因为我可以删除前端添加$符号的代码
42.35
10.25
1200
java.lang.NumberFormatException: Valid number not found in value: obviously not valid input
    at soj25597001.Program.forceBigDecimal(Program.java:50)
    at soj25597001.Program.main(Program.java:22)
42
0.3
42.35