如何在Java中解决NumberFormatException?

12 浏览
0 Comments

如何在Java中解决NumberFormatException?

表字段

字段: 银行账号
类型: int(100)

这是我的'账户'模型,具有setter/getter方法

@Entity
@Table(name = "account")
public class Account {
@Column(name = "Pharmacy_Account_No")
private Integer pharmacyAccountNo;
public Integer getPharmacyAccountNo() {
    return pharmacyAccountNo;
}
public void setPharmacyAccountNo(Integer pharmacyAccountNo) {
    this.pharmacyAccountNo = pharmacyAccountNo;
   }
}

当我插入"1234567891"时,它会成功添加。但是当我插入"123456789111111"时,它会抛出NumberFormatException异常

插入代码:
pharmacyAccount.setPharmacyAccountNo(request.getParameter("pharmacyAccountNo") != null
                        && !request.getParameter("pharmacyAccountNo").equals("")
                                ? Integer.valueOf(request.getParameter("pharmacyAccountNo")) : 0);

堆栈跟踪如下:

java.lang.NumberFormatException: For input string: "123456789111111"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.pharmacy.ServiceImpl.PharmacyServiceImpl.savePharmacyAccount(PharmacyServiceImpl.java:977)
at com.pharmacy.ServiceImpl.PharmacyServiceImpl.savePharmacy(PharmacyServiceImpl.java:838)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy29.savePharmacy(Unknown Source)
at com.pharmacy.Controller.PharmacyController.savePharmacy(PharmacyController.java:232)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:99)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

0
0 Comments

在Java中,NumberFormatException是一个常见的异常,表示无法将一个字符串转换为有效的数字类型(如int、float等)。在这里,我们可以看到错误的根本原因是使用了一个超出int类型范围的数字。解决方法是将其改为使用Long类型。

根据Oracle官方文档,int是一个32位有符号的二进制补码整数类型,其最小值是-231,最大值是231-1。在Java SE 8及更高版本中,可以使用int类型来表示无符号的32位整数,其最小值是0,最大值是232-1。如果要使用int类型作为无符号整数,可以使用Integer类。所以,对于超出int类型范围的数字,应该使用Long类型。

Long类型是一个64位的有符号整数类型,其最小值为-2^63,最大值为2^63-1(即-9,22337E+18到9,22337E+18)。

此外,Java和C/C++之间还存在一些差异。在Java中,数据类型是标准的,因为所有的代码都在JVM上运行。而在C/C++中,数据类型会根据机器的体系结构而变化。

所以,解决这个NumberFormatException的问题,只需要将数字类型从int改为Long即可。BigInteger是用于更大的数字,但在这种情况下并不需要使用它。

0
0 Comments

在Java中,当我们尝试将一个超出范围的数字转换为整数时,可能会遇到NumberFormatException异常。这通常是由于数字超出了整数类型的范围所致。

在Java中,整数类型有不同的范围。以下是各种整数类型的范围示例:

signed 8位整数:-128到+127

signed 16位整数:-32,768到+32,767

signed 32位整数:-2,147,483,648到+2,147,483,647

signed 64位整数:-9,223,372,036,854,775,808到+9,223,372,036,854,775,807

unsigned 8位整数:0到+255

unsigned 16位整数:0到+65,535

unsigned 32位整数:0到+4,294,967,295

unsigned 64位整数:0到+18,446,744,073,709,551,615

如果我们尝试将一个超出这些范围的数字转换为整数,就会出现NumberFormatException异常。例如,如果我们尝试将字符串"123456789111111"转换为整数,就会出现此异常,因为该数字超出了整数类型的最大值。

解决这个问题的方法是确保将要转换的数字在合理的范围内。如果我们知道要转换的数字可能超出整数类型的范围,我们可以使用更大的整数类型,如长整型(long)。如果我们不确定要转换的数字的范围,我们可以使用try-catch块来捕获NumberFormatException异常,并在出现异常时采取适当的措施。

总结起来,当我们尝试将超出整数类型范围的数字转换为整数时,会出现NumberFormatException异常。为了解决这个问题,我们应该确保要转换的数字在合理的范围内,并可以使用更大的整数类型或使用try-catch块来处理异常。

0
0 Comments

如何解决Java中的NumberFormatException?

在Java中,NumberFormatException是一个常见的错误,它表示由于字符串无法被转换为数字而导致的异常。这通常发生在尝试将一个字符串转换为一个数字类型(如int、long等)时。

出现NumberFormatException的原因是由于字符串表示的数字超出了数字类型的范围。在Java中,整数类型int的最大值为2147483647,最小值为-2147483648。当字符串表示的数字超过这个范围时,就会抛出NumberFormatException异常。

解决NumberFormatException的方法是将数字类型从int更改为long。long类型的最大值是9223372036854775807,最小值是-9223372036854775808。通过使用long类型,我们可以扩大数字的范围,从而避免NumberFormatException异常的出现。

因此,建议在处理可能超出int范围的数字时,将类型更改为long。这样一来,即使字符串表示的数字超过了int的范围,也可以成功地将其转换为long类型,而不会抛出NumberFormatException异常。

下面是一个示例代码,演示了如何将字符串转换为long类型以避免NumberFormatException异常的出现:

String numberString = "2147483648";
long number = Long.parseLong(numberString);

在上面的示例中,我们将字符串"2147483648"转换为long类型的数字。由于这个数字超过了int的范围,如果我们尝试将其转换为int类型,就会抛出NumberFormatException异常。但是,通过将类型更改为long,我们可以成功地将其转换为数字,而不会出现异常。

总之,要解决Java中的NumberFormatException异常,我们需要注意数字的范围。如果字符串表示的数字超出了数字类型的范围,应该将类型更改为更大的类型,如从int更改为long。这样一来,我们就可以成功地将字符串转换为数字,而不会抛出NumberFormatException异常。

0