博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.lang.NumberFormatException: For input string: "undefined"
阅读量:5736 次
发布时间:2019-06-18

本文共 7635 字,大约阅读时间需要 25 分钟。

在将字符串转换为数字时导致此错误,解决此问题的思路:1、添加Try catch语句,2、判断字符串是否为数字,将介绍java中判断字符串是否为数字的方法的几种方法。

完整错误信息:


java.lang.NumberFormatException: For input string: "undefined"    at java.lang.NumberFormatException.forInputString(Unknown Source)    at java.lang.Integer.parseInt(Unknown Source)    at java.lang.Integer.valueOf(Unknown Source)    at com.xinghe.frame.base.web.BaseSpringController.getInteger(BaseSpringController.java:123)    at com.xinghe.edu.web.BaseAdminController.setFieldValues(BaseAdminController.java:340)    at com.xinghe.platform.action.ResResourcePublic.saveComment(ResResourcePublic.java:504)    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.invokeHandlerMethod(HandlerMethodInvoker.java:177)    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at com.auth.filter.AuthorizationFilter.doFilterInternal(AuthorizationFilter.java:77)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at com.auth.filter.AuthenticationFilter.doFilterInternal(AuthenticationFilter.java:115)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at com.xinghe.activate.filter.ActivateFilter.doFilterInternal(ActivateFilter.java:52)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)    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)

发生错误的底层代码:


java.lang.NumberFormatException.forInputString(Unknown Source)    at java.lang.Integer.parseInt(Unknown Source)    at java.lang.Integer.valueOf(Unknown Source)

发生错误原因:


由于AJAX调用的时候对应的参数未设置,导致后台获取到的是字符串”undefined”,在将它转换成Integer类型的时候出的错。

解决办法:


AJAX调用的时候设置对应的参数;

添加Try catch语句;
最好的做法还是应该在进行参数类型转换的时候先对待转换的值做一下检测,看是否符合目标类型格式;

java中判断字符串是否为数字的方法的几种方法


1.用JAVA自带的函数

public static boolean isNumeric(String str){  for (int i = 0; i < str.length(); i++){   System.out.println(str.charAt(i));   if (!Character.isDigit(str.charAt(i))){    return false;   }  }  return true; }

2.用正则表达式

首先要import java.util.regex.Pattern 和 java.util.regex.Matcher

public boolean isNumeric(String str){    Pattern pattern = Pattern.compile("[0-9]*");    Matcher isNum = pattern.matcher(str);   if( !isNum.matches() ){       return false;    }    return true; }

3.使用org.apache.commons.lang

org.apache.commons.lang.StringUtils;

boolean isNunicodeDigits=StringUtils.isNumeric(“aaa123456789”);
下面的解释:isNumeric

public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.null will return false. An empty String ("") will return true. StringUtils.isNumeric(null)   = false StringUtils.isNumeric("")     = true StringUtils.isNumeric("  ")   = false StringUtils.isNumeric("123")  = true StringUtils.isNumeric("12 3") = false StringUtils.isNumeric("ab2c") = false StringUtils.isNumeric("12-3") = false StringUtils.isNumeric("12.3") = falseParameters:str - the String to check, may be null Returns:true if only contains digits, and is non-null

几种实现方式比较

上面三种方式中,第二种方式比较灵活。

第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;

而第二方式则可以通过修改正则表达式实现校验负数,将正则表达式修改为“^-?[0-9]+”即可,修改为“-?[0-9]+.?[0-9]+”即可匹配所有数字。

转载地址:http://pvwzx.baihongyu.com/

你可能感兴趣的文章
mysql权限管理
查看>>
我的友情链接
查看>>
让你快速上手的Glide4.x教程
查看>>
浮动和清除(闭合)浮动
查看>>
微信小程序注册流程
查看>>
LR录制脚本时IE打不开的原因
查看>>
类的基础
查看>>
微博自动化测试
查看>>
Sublime Text 2.0.2,Build 2221注册码
查看>>
js scroll事件
查看>>
day08 文件操作
查看>>
最长递增子序列 动态规划
查看>>
使用列表
查看>>
原生CSS设置网站主题色—CSS变量赋值
查看>>
webpack 4.0 中 clean-webpack-plugin 的使用
查看>>
数据库神器:Navicat Premium
查看>>
WPF
查看>>
Best website for Photogrammetry
查看>>
中文词频统计
查看>>
POJ 2236 Wireless Network (并查集)
查看>>