在将字符串转换为数字时导致此错误,解决此问题的思路:

1、添加 try catch语句

2、判断字符串是否为数字,将介绍java中判断字符串是否为数字的方法的几种方法

发生错误的代码:

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.char(i))){
return false;
}
}
return true;
}

2、用正则表达式

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

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

3、使用org.apache.commons.lang

java.lang.NumberFormatException:For input string:"undefined"的更多相关文章

  1. java.lang.NumberFormatException: For input string: "1608020001 " 错误

    错误: java.lang.NumberFormatException: For input string: "1608020001 "    at java.lang.Numbe ...

  2. java.lang.NumberFormatException: For input string: "Y"

    nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  ...

  3. mybatis 报错:Caused by: java.lang.NumberFormatException: For input string

    mybatis的if标签之前总是使用是否为空,今天要用到字符串比较的时候遇到了困难,倒腾半天,才在一个论坛上找到解决方法.笔记一下,如下: 转自:https://code.google.com/p/m ...

  4. java.lang.NumberFormatException: For input string:"filesId"

    做项目时候,页面获取出现了这个问题.找了好久一直以为是我字段或者是数据库字段问题导致引起的. 最后才发现是 struts2中jsp我写错了一个参数,一直导致报错.后来改了就好了. 当大家遇到这个问题的 ...

  5. 解决java.lang.NumberFormatException: For input string: "id"

    今天,项目突然报"java.lang.NumberFormatException:For input string:"id"",项目框架是spring,spri ...

  6. MyBatis报错:Caused by: java.lang.NumberFormatException: For input string: "XX"

    <select id="sltTreatment" resultType="com.vitaminmd.sunny.core.bo.Treatment"& ...

  7. Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615"

    问题:Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615" 原因 ...

  8. Redis集群环境使用的是redis4.0.x的版本,在用java客户端jedisCluster启动集群做数据处理时报java.lang.NumberFormatException: For input string: "7003@17003"问题解决

    java.lang.NumberFormatException: For input string: "7003@17003" at java.lang.NumberFormatE ...

  9. hadoop ha环境下的datanode启动报错java.lang.NumberFormatException: For input string: "10m"

    hadoop ha环境启动start-dfs.sh的时候datanode启动不了,并且报错. [hadoop@datanode2 ~]$ cat /home/hadoop/hadoop-2.7.3/l ...

随机推荐

  1. HDU 6342 Expression in Memories(模拟)多校题解

    题意:给你一个规则,问你写的对不对. 思路:规则大概概括为:不能出现前导零,符号两边必须是合法数字.我们先把所有问号改好,再去判断现在是否合法,这样判断比一边改一边判断容易想. 下面的讲解问号只改为+ ...

  2. python 匹配指定后缀的文件名

    import glob x=glob.glob('*.py') print(x)

  3. android:点击popupwindow以外区域 popupwindow自动消失

    方法一(这种方法可以处理popupwindows dimiss的时候一些其他的操作,比如让其他控件的隐藏,消失等): 代码如下popupWindow.setFocusable(false);//foc ...

  4. Linux Mint 18.1安装nvidia驱动

    硬件环境:Dell Inspiron 7557笔记本(i7,8G,GTX960M) 软件环境:Linux Mint 18.1(基于Ubuntu 16.04) 问题描述: Linux Mint 18.1 ...

  5. freemarker多个checkbox一个以上被选中示例

    <tr> <td class="handColumn" colspan="5" > <#list deptHandNotConta ...

  6. JS object(对象)的学习汇总

    Object(对象)是在所有的编程语言中都十分重要的一个概念,对于事物我们可以把他们看作是一个对象,而每一个事物都有自己的表示的属性和对于某一信息作出的相应的操作.而这些东西就变成了事物的属性和方法. ...

  7. Digitalocean+DNSPod搭建Meteor.js博客Telescope.js

    1. 什么是Meteor.js 基于Node.js的一个快速开发平台. 简言之,Node.js>Meteor.js 对等于Ruby>Ruby on Rails的关系. 官网:http:// ...

  8. 008PHP文件处理——文件操作r w (用的比较多) a x(用的比较少) 模式 rewind 指针归位:

    <?php /** *文件操作r w (用的比较多) a x(用的比较少) 模式 rewind 指针归位: */ /*$a=fopen('a.txt','r'); echo fread($a,f ...

  9. JavaScript运算符:递增和递减(++i,--i 和 i++,i-- 的区别)

    递增和递减操作符直接借鉴自C,而且各有两个版本:前置型 (递增 ++i ,递减 --i )和 后置型 (递增 i++ ,递减 i-- ).书本上对两者的定义是:前置型应该位于要操作的变量之前,而后置型 ...

  10. Python - Learn Note (2)

    Python注释 Python的注释以#开头,后面的文字直到行尾都算注释 Python基本数据类型 整数.浮点数(浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置 ...