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

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. POJ 3169 Layout(差分约束+最短路)题解

    题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...

  2. 使用向量化的 if:ifelse

    进行分支计算的一个替代方法是 ifelse( ).这个函数接收一个逻辑向量作为判定条件,并且返回一个向量.对于逻辑判定条件内的每一个元素,若是 TRUE,则选择第 2个参数 yes 中所对应的元素:若 ...

  3. VC6_预编译头

    1.去掉 使用预编译头"stdafx.h" VC6 --> Project --> Settings.. --> C/C++选项卡 --> "Ca ...

  4. githubpage+hexo构建自己的个人博客

    就这有这么一天,我感觉博客园的博客页面实在太low了.完全跟不上现在潮流. https://segmentfault.com/a/1190000004947261 http://coolcao.com ...

  5. Mac Hadoop2.6(CDH5.9.2)伪分布式集群安装

    操作系统: MAC OS X 一.准备 1. JDK 1.8 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  6. Autofac Mvc注入

    private void DependencyInjection() { var builder = new ContainerBuilder(); builder.RegisterControlle ...

  7. 管道 && 消息队列 && 共享内存

    http://blog.csdn.net/piaoairy219/article/details/17333691 1. 管道 管道的优点是不需要加锁. 缺点是默认缓冲区太小,只有4K. 一个管道只适 ...

  8. LINUX中的RCU机制的分析

    RCU机制是Linux2.6之后提供的一种数据一致性访问的机制,从RCU(read-copy-update)的名称上看,我们就能对他的实现机制有一个大概的了解,在修改数据的时候,首先需要读取数据,然后 ...

  9. Maven入门-1.介绍及搭建开发环境

    1.介绍1.1 Maven和Ant的比较?2.搭建Maven开发环境2.1 配置环境变量2.2 测试安装是否成功2.3 配置Maven的本地仓库位置3.Maven的约定4.Maven项目类型5.Mav ...

  10. EM算法及其应用(一)

    EM算法及其应用(一) EM算法及其应用(二): K-means 与 高斯混合模型 EM算法是期望最大化 (Expectation Maximization) 算法的简称,用于含有隐变量的情况下,概率 ...