如何在Java中将String转换为int

在本教程中,我们将看到将Java中的String转换为int(或Integer)的各种方法。

您可以使用以下任何一种方式:

–使用Integer.parseInt(string)

–使用Integer.valueof(string)

–使用Apache Commons NumberUtils.toInt(string)

–使用Apache Commons NumberUtils.createInteger(string)

–使用Guava库的Ints.tryParse(string)方法

–使用Integer.decode(string)

–使用新的整数(字符串)

使用Integer.parseInt(string)

String empId1 = "1001";
int intEmpId1 = Integer.parseInt(empId1);
System.out.println(intEmpId1);Output :
1001

在以下情况下,Integer.parseInt()将引发NumberFormatException:

/ Alphabets in the input.Integer.parseInt("100AB");//Input number is greater than the Integer range.Integer.parseInt("2147483648");//Number with decimalInteger.parseInt("1.1"); //empty StringInteger.parseInt(""); //Blank spaceInteger.parseInt(" ");

使用Integer.valueof(string)

String empId2 = "2001";Integer integerEmpId2 = Integer.valueOf(empId2);
System.out.println(integerEmpId2);Output :
2001

使用Apache Commons NumberUtils.toInt(string)

String empId3 = "3001";
int intEmpId3 = NumberUtils.toInt(empId3);
System.out.println(intEmpId3);Output :
3001int intEmpId4 = NumberUtils.toInt(null);
System.out.println(intEmpId4);Output :
0int intEmpId5 = NumberUtils.toInt("1001ABC");
System.out.println(intEmpId5);Output :
0int intEmpId6 = NumberUtils.toInt("1001ABC", 10);
System.out.println(intEmpId6);Output :
10

使用Apache Commons NumberUtils.createInteger(string)

String empId4 = "4001";
Integer integerEmpId7 = NumberUtils.createInteger(empId4);
System.out.println(integerEmpId7);Output :
4001

使用Guava库的Ints.tryParse(string)方法

String empId5 = "5001";
Integer integerEmpId8 = Ints.tryParse(empId5);System.out.println(integerEmpId8);Output :
5001

使用Integer.decode(string)

String empId6 = "6001";
Integer integerEmpId9 = Integer.decode(empId6);
System.out.println(integerEmpId9);Output :
6001

使用新的整数(字符串)

String empId7 = "7001";
Integer integerEmpId10 = new Integer(empId7);
System.out.println(integerEmpId10);Output :
7001

但是,请记住,从Java9开始不推荐使用此Integer构造函数。

完成程序

package com.blogspot.javasolutionsguide.stringtointexample;import org.apache.commons.lang3.math.NumberUtils;
import com.google.common.primitives.Ints;public class StringToInt {public static void main(String[] args) {String empId1 = "1001";int intEmpId1 = Integer.parseInt(empId1);System.out.println(intEmpId1);String empId2 = "2001";Integer integerEmpId2 = Integer.valueOf(empId2);System.out.println(integerEmpId2);String empId3 = "3001";int intEmpId3 = NumberUtils.toInt(empId3);System.out.println(intEmpId3);int intEmpId4 = NumberUtils.toInt(null);System.out.println(intEmpId4);  int intEmpId5 = NumberUtils.toInt("1001ABC");System.out.println(intEmpId5);int intEmpId6 = NumberUtils.toInt("1001ABC", 10);System.out.println(intEmpId6);String empId4 = "4001";Integer integerEmpId7 = NumberUtils.createInteger(empId4);System.out.println(integerEmpId7);String empId5 = "5001";Integer integerEmpId8 = Ints.tryParse(empId5);System.out.println(integerEmpId8);String empId6 = "6001";Integer integerEmpId9 = Integer.decode(empId6);System.out.println(integerEmpId9);String empId7 = "7001";Integer integerEmpId10 = new Integer(empId7);System.out.println(integerEmpId10);// Alphabets in the input.Integer.parseInt("100AB");//Input number is greater than the Integer range.Integer.parseInt("2147483648");//Number with decimalInteger.parseInt("1.1"); //empty StringInteger.parseInt(""); //Blank spaceInteger.parseInt(" "); }
}

使用的依赖项:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version>
</dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>16.0.1</version></dependency>

摘要

因此,在本教程中,我们看到了

–如何在Java中将String转换为int(或Integer)。 –在大多数情况下,如果需要int或Integer,则可以使用Integer.parseInt()和Integer.valueOf() 分别来自String并希望避免依赖第三方库。 – NumberUtils。 toInt ()可用于不希望我们的程序由于NumberFormatException而失败的情况。

翻译自: https://www.javacodegeeks.com/2020/03/how-to-convert-string-to-int-in-java.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/340135.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java回调spring接口_Spring Boot启动过程及回调接口汇总

启动顺序Spring boot的启动代码一般是这样的&#xff1a;123456SpringBootApplicationpublic class SampleApplication {public static void main(String[] args)throws Exception {SpringApplication.run(SampleApplication.class, args);}}初始化SpringApplication1、SpringA…

python面向对象编程138讲_Python面向对象编程简单讲解

学习目标&#xff1a;1-面向对象编程2-类和对象3-对象的属性和方法4-类的特性Python面向对象编程一种编程程序的设计思想&#xff0c;把对象作为程序的基本单元&#xff0c;一个对象包含了数据和操作数据的函数。把计算机程序视为一组对象的集合&#xff0c;而每个对象都可以接…

jboss 程序位置_介绍JBoss BPM Suite安装程序

jboss 程序位置本周&#xff0c;我们想向您介绍JBoss BRMS和JBoss BPM Suite产品随附的一个鲜为人知的安装程序组件。 请注意&#xff0c;当前所有的演示项目都要求您下载JBoss BPM Suite可部署的eap zip产品文件和JBoss EAP 6.1.1 zip产品文件。 展望未来&#xff0c;我们将迁…

java换成中文_如果我们的编程替换成中文会变成怎样?

首先大概的看一下中文编码&#xff1a;你以为会写中文就会编程吗&#xff1f;这就像你以为会写汉字就会写出好文章一样。编程是和机器沟通&#xff0c;因此要用机器的语言而不是人类的语言。最早的程序就是0和1的数字&#xff0c;不是中文也不是英文。以前的程序员&#xff0c;…

高等数学公式大全_高中物理知识思维导图大全,赶紧收藏!

物理作为理综的重中之重&#xff0c;物理的学习一直是广大考生的难点。如何快捷高效的掌握物理知识点是高考复习的重点之一&#xff0c;根据高中物理三年知识点用思维导图的方式&#xff0c;来助大家掌握物理知识点。运动的描述 重力 基本相互作用 相互作用 牛顿运动定律 力的合…

HTTP方法:幂等性和安全性

幂等性和安全性是HTTP方法的属性。 HTTP RFC定义了这些属性&#xff0c;并告诉我们哪些HTTP方法是安全且幂等的。 服务器应用程序应确保正确执行安全和幂等的语义&#xff0c;如客户端期望的那样。 安全的HTTP方法 如果HTTP方法不更改服务器状态&#xff0c;则认为它们是安全的…

java a[i].setx(-1);_java – setX和setY在尝试定位图像时不起作用

我在使用setX上的setX和setY为我的JavaFX程序中定位图像时遇到问题.我不确定是什么问题&#xff1f;感谢任何给予的帮助&#xff01;这是我的代码&#xff1a;Image rocket2 new Image("img/Rocket.png");ImageView iv1 new ImageView(rocket2);iv1.setX(60);iv1.s…

go环境搭建_学习的golang第一步,搭建我们运行的环境,go! go! go

这是Golang教程系列中的第一个教程。本教程介绍了Go&#xff0c;并讨论了选择Go优于其他编程语言的优势。我们还将学习如何在Mac OS&#xff0c;Windows和Linux中安装Go。介绍Go也称为Golang是由Google开发的一种开源&#xff0c;编译和静态类型的编程语言。创造Go的关键人物是…

如何在Java中将数组转换为列表

你好朋友&#xff0c; 在本教程中&#xff0c;我们将学习将数组转换为List的各种方法。 package com.blogspot.javasolutionsguide;import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils;import java.util.ArrayList; import ja…

html5网页制作代码_HTML5的网页设计教程

关注小编&#xff0c;教你如何制作网页HTML5是超文本标记语言(HyperText Markup Language)的第五代版本&#xff0c;它是书写网页代码的一种规范、一种标准。它通过标记符号来标记要显示的网页中的各个部分。浏览器根据这个标准显示其中的内容(如&#xff1a;文字如何处理&…

java命令框编译代码的方式_在命令行模式下如何编译运行Java代码

由于已经习惯了在Windows环境中进行开发&#xff0c;最近在尝试将开发环境转移到Linux上面。遇到一篇值得参考的外文&#xff0c;特翻译记录一下。【只翻译了部分】一直被集成开发环境和自动构建工具“溺爱”着&#xff0c;我最近意识到我居然一点儿都不会在命令行模式下去运行…

hibernate jpa_JPA /Hibernate刷新策略初学者指南

hibernate jpa介绍 在我以前的文章中&#xff0c;我介绍了实体状态转换 对象关系映射范例。 当刷新当前持久性上下文时&#xff0c;所有管理实体状态转换都将转换为关联的数据库语句。 Hibernate的刷新行为并不总是像人们想象的那样明显。 后写 Hibernate尝试将持久性上下文刷…

aop+注解 实现对实体类的字段校验_SpringBoot实现通用的接口参数校验

来自&#xff1a;掘金&#xff0c;作者&#xff1a;cipher链接&#xff1a;https://juejin.im/post/5af3c25b5188253064651c76原文链接&#xff1a;http://www.ciphermagic.cn/spring-boot-aop-param-check.html本文介绍基于Spring Boot和JDK8编写一个AOP&#xff0c;结合自定义…

java基础分享_一、java基础教程

1、java是一门比较纯粹的面向对象编程语言&#xff0c;所以java的所有代码都必须写在类的内部。1.1 java的可执行文件后缀名是".java"&#xff0c;例如HelloWorld.java&#xff0c;并且每个可执行文件内部&#xff0c;必须有且仅有一个public公共类/公共接口/公共抽象…

Spring Boot自定义横幅生成

每当我们启动Spring Boot应用程序时&#xff0c;都会显示如下所示的文本消息。 这称为横幅。 现在&#xff0c;如果我们可以创建一个特定于我们的Spring Boot应用程序的自定义横幅并使用它代替默认的Spring Boot横幅&#xff0c;那将不是一件很棒的事。 有很多方法可以生成和使…

java简单递归算法_java递归算法的简单示例

java递归算法的简单示例今天参加笔试&#xff0c;要求用递归实现1357……没写出来&#xff0c;蛋疼。。。。下面复习一下&#xff0c;自己尝试着写还是没有写出来。。。。。没有写出来的关键是自己不知道该如何实现求和、实现出口递归实现的关键是&#xff1a;1.出口的设计2.递…

java等待_Java学习:等待唤醒机制

等待唤醒机制线程的状态NEW 至今尚未启动的线程处于这种状态RUNNABLE 正在Java虚拟机中执行的线程处于这种状态BLOCKED 受阻塞并等待某个监视器锁的线程处于这种状态WAITING 无限期的等待另一个线程来执行某一待定操作的线程处于这种状态TIMED_WAITNG 等待另一个线程来执行取…

游戏ai 行为树_游戏AI –行为树简介

游戏ai 行为树游戏AI是一个非常广泛的主题&#xff0c;尽管有很多资料&#xff0c;但我找不到能以较慢且更易理解的速度缓慢介绍这些概念的东西。 本文将尝试解释如何基于行为树的概念来设计一个非常简单但可扩展的AI系统。 什么是AI&#xff1f; 人工智能是参与游戏的实体表现…

java适配器模式应用_java笔记--适配器模式的运用

适配器模式的运用主要应用&#xff1a;可以在符合OCP原则(开笔原则)的基础上&#xff0c;为类增加新的功能。在现有功能上添加新的功能时无须修改原有功能代码。主要角色&#xff1a;目标角色&#xff1a;就是期待得到的接口&#xff0c;如给汽车添加GPS的接口;源角色&#xff…

java构造器_Java构造器就是这么简单!

前言理解构造器之前&#xff0c;首先我们需要了解Java中为什么要引入构造器&#xff0c;以及构造器的作用。在很久之前&#xff0c;程序员们编写C程序总会忘记初始化变量&#xff08;这真的是一件琐碎但必须的事&#xff09;&#xff0c;C引入了 构造器(constructor) 的概念&am…