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

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. reason: image not found的解决方案

    在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...

  2. Redis之数据备份与恢复

    Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...

  3. Gym - 101334C 3514 无向仙人掌

    http://codeforces.com/gym/101334/attachments 题意: 判断是否是仙人掌图并且连通,如果是的话则计算出它有多少个连通子图也是仙人掌. 思路:连通子图也就是我们 ...

  4. javascript中back(-1)和go(-1)的区别

    javascript中back(-1)和go(-1)的区别 一.总结 一句话总结: 数据 history.back(-1):直接返回当前页的上一页,数据全部消息,是个新页面 history.go(-1 ...

  5. HDU 4540 线性DP

    威威猫系列故事——打地鼠 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  6. IOS-static cell 与 dynamic cell 混合使用

    static cell 与 dynamic cell 混合使用 关于静态cell与动态cell的混合使用,google一下便会有很多相关文章,这里也是看过一些前辈的经验(已经忘记具体是从哪篇文章得到的 ...

  7. SSH-Auditor:一款SSH弱密码探测工具

    SSH-Auditor:一款SSH弱密码探测工具 freebuf 2018-09-16  ssh-auditor是一款可帮助你探测所在网络中ssh弱密码的工具. 特性 以下操作ssh-auditor都 ...

  8. PHP:第五章——字符串与数组及其他函数

    <?php header("Content-Type:text/html;charset=utf-8"); //1.str_split——将字符串转换为数组. /*$str= ...

  9. VS2017打包安装程序

    VS2017 并不自带安装部署项目,需要在[扩展和更新]中安装插件:Microsoft Visual Studio 2017 Installer Projects(现更名为Microsoft Visu ...

  10. 软件工程firstblood

    https://github.com/happyeven/WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有wc.exe 的 ...