google-code-export / nutz

Automatically exported from code.google.com/p/nutz
Apache License 2.0
1 stars 1 forks source link

String2Number 不够智能 #372

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
如果String中有空格什么的,不能转成int

org.nutz.castor.FailToCastObjectException: Fail to cast '3 ' to <int>
    at org.nutz.castor.castor.String2Number.cast(String2Number.java:36)
    at org.nutz.castor.castor.String2Number.cast(String2Number.java:19)
    at org.nutz.castor.Castors.cast(Castors.java:257)
    at org.nutz.castor.Castors.castTo(Castors.java:317)
    at org.nutz.lang.inject.InjectBySetter.inject(InjectBySetter.java:23)
    at org.nutz.mvc.adaptor.injector.Node.get(Node.java:69)
    at org.nutz.mvc.adaptor.injector.ObjectPairInjector.get(ObjectPairInjector.java:35)
    at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:123)
    at org.nutz.mvc.invoker.ActionInvokerImpl.invoke(ActionInvokerImpl.java:236)
    at org.nutz.mvc.ActionInvoking.invoke(ActionInvoking.java:37)
    at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:73)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.nutz.castor.castor.String2Number.cast(String2Number.java:30)
    ... 22 more
Caused by: java.lang.NumberFormatException: For input string: "3 "
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:456)
    at java.lang.Integer.<init>(Integer.java:620)
    ... 27 more

Original issue reported on code.google.com by juq...@gmail.com on 30 Jan 2011 at 3:01

GoogleCodeExporter commented 9 years ago
有必要容忍这个错误>??

Original comment by wendal1985@gmail.com on 30 Jan 2011 at 3:10

GoogleCodeExporter commented 9 years ago
额...
之所以会出这个错,是因为JSP用EL生成数据的时候不小心弄的
我个人还是觉得有比较好,虽然计算机把"  3  
"看成字符串,但是人的肉眼一般还是把它当成数字的.
而大家使用这个类型转换,更多的是希望忽略空格吧.

话又说回来Integer.parseInt("3 "), new Integer("3 "), Integer.valueOf("3 
")都会抛异常的说....

Original comment by juq...@gmail.com on 30 Jan 2011 at 3:19

GoogleCodeExporter commented 9 years ago
我也觉得没必要

而且,如果这这样, String2Date 神码的,也得 trim 喽。

Original comment by zozoh...@gmail.com on 30 Jan 2011 at 5:52

GoogleCodeExporter commented 9 years ago
下个版本发布前,如果没有更多的意见,这个 ISSUE 
将被设置为 WonFix + FAQ

Original comment by zozoh...@gmail.com on 10 Feb 2011 at 3:11

GoogleCodeExporter commented 9 years ago
关于这个问题,同意这样的处理,我也不再对这个问题提更��
�的意见,就象wendal1985所说,没必要容忍这个错误。再加上JAV
A本身Integer的实现就对这个问题做了佐证

Original comment by juq...@gmail.com on 10 Feb 2011 at 3:22

GoogleCodeExporter commented 9 years ago
收到

Original comment by zozoh...@gmail.com on 10 Feb 2011 at 3:45