2014-09-03 90 views
0

传递小数时,我收到“不是有效的char构造函数输入”。我的代码如下。我研究并发现,通过从BigDecimal更改为Formatdeciaml解决了这个问题,但我无法做到这一点,因为我的映射器也需要更改。任何帮助,将不胜感激!!传递小数时接收“无效的char构造函数输入”错误

控制器

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) 
+1

你确定你传递通过解析在http://docs.oracle.com/ specced语法的字符串javase/7/docs/api/java/math/BigDecimal.html#BigDecimal(java.lang.String)? – jdphenix 2014-09-03 21:14:53

+0

你能发布导致异常的输入字符串吗? – jdphenix 2014-09-03 21:22:51

+0

我只是重申,前面的代码是addidng到我的输入字符串传递字符串是1.50美元。 – user2149910 2014-09-03 21:30:24

回答

1

这个问题的最终目的是采取未知的输入,并将其解析成一个BigDecimal。您可以使用正则表达式从String中获取数字值。

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

输出:

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