gpt4 book ai didi

java - Long.parseLong 给出 java.lang.NumberFormatException

转载 作者:行者123 更新时间:2023-12-02 07:26:42 30 4
gpt4 key购买 nike

所以我尝试了很多东西,但我无法解决这个问题。 Service 类中的 id 数据类型为 long。我试图将 serviceAuth 转换为 long,但它抛出一个 java.lang.NumberFormatException。我该如何解决这个问题?

String[] serviceList = getUser.serviceList.split(",");

for(String serviceAuth: serviceList) {
Long temp = Long.parseLong(serviceAuth.toString());
Criteria ctr = sessionFactory.getCurrentSession().createCriteria(Service.class)
.add(Restrictions.eq("id",temp));
}

serviceList 看起来像这样 5,18,19。它是从 csv 文件加载的。

当我在 for 循环中打印值时,它看起来像这样:

1
2
14
15

错误报告:

 java.lang.NumberFormatException: For input string: ""
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Long.parseLong(Long.java:601)
java.lang.Long.parseLong(Long.java:631)
org.x.y.gateway.MainController.getUsers(MainController.java:1433)
org.x.y.gateway.MainController$$FastClassBySpringCGLIB$$6e5db2d9.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
org.x.y.gateway.MainController$$EnhancerBySpringCGLIB$$149bedb6.getUsers(<generated>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
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:781)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.x.y.gateway.CrossOriginRequest.doFilter(CrossOriginRequest.java:18)

最佳答案

您的问题是 split() 创建的字符串之一为空或包含空格,例如

package de.scrum_master.app;

public class Application {
public static void main(String[] args) {
String[] serviceList = ",15,18,19".split(",");
for (String serviceAuth : serviceList) {
Long temp = Long.parseLong(serviceAuth.toString());
System.out.println(temp);
}
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at de.scrum_master.app.Application.main(Application.java:7)

更新:如何解决问题

你应该做两件事:

  • 使拆分正则表达式对前导和尾随空格更加稳定。
  • 跳过循环中仍可能出现在数组开头的 "" 的值。
package de.scrum_master.app;

public class Application {
public static void main(String[] args) {
String[] serviceList = " , 15 , 18 , 19, ".split("[\\s,]+");
for (String serviceAuth : serviceList) {
if ("".equals(serviceAuth))
continue;
Long temp = Long.parseLong(serviceAuth.toString());
System.out.println(temp);
}
}
}
15
18
19

关于java - Long.parseLong 给出 java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27597841/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com