Core Java

How to convert String to int in Java

In this tutorial, we will see the various ways in which we can convert String to int(or Integer) in Java.

You can use any of the following ways :

– Using Integer.parseInt(string)

– Using Integer.valueof(string)

– Using Apache commons NumberUtils.toInt(string)

– Using Apache commons  NumberUtils.createInteger(string)

– Using Guava library’s Ints.tryParse(string) method

– Using Integer.decode(string)

– Using new Integer(string)

Using Integer.parseInt(string)

1
2
3
4
5
6
String empId1 = "1001";
int intEmpId1 = Integer.parseInt(empId1);
System.out.println(intEmpId1);
 
Output :
1001

Integer.parseInt() will throw NumberFormatException in following cases:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
/ Alphabets in the input.
 Integer.parseInt("100AB");
 
//Input number is greater than the Integer range.
 Integer.parseInt("2147483648");
   
//Number with decimal
 Integer.parseInt("1.1");
 
 //empty String
 Integer.parseInt("");
   
 //Blank space
 Integer.parseInt(" ");

Using Integer.valueof(string)

1
2
3
4
5
6
7
String empId2 = "2001";
 
Integer integerEmpId2 = Integer.valueOf(empId2);
System.out.println(integerEmpId2);
 
Output :
2001

Using Apache commons NumberUtils.toInt(string)

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
String empId3 = "3001";
int intEmpId3 = NumberUtils.toInt(empId3);
System.out.println(intEmpId3);
 
Output :
3001
 
   
int intEmpId4 = NumberUtils.toInt(null);
System.out.println(intEmpId4);
 
Output :
0
 
   
int intEmpId5 = NumberUtils.toInt("1001ABC");
System.out.println(intEmpId5);
 
Output :
0
 
   
int intEmpId6 = NumberUtils.toInt("1001ABC", 10);
System.out.println(intEmpId6);
 
Output :
10

Using Apache commons  NumberUtils.createInteger(string)

1
2
3
4
5
6
String empId4 = "4001";
Integer integerEmpId7 = NumberUtils.createInteger(empId4);
System.out.println(integerEmpId7);
 
Output :
4001

Using Guava library’s Ints.tryParse(string) method

1
2
3
4
5
6
7
String empId5 = "5001";
Integer integerEmpId8 = Ints.tryParse(empId5);
 
System.out.println(integerEmpId8);
 
Output :
5001

Using Integer.decode(string)

1
2
3
4
5
6
String empId6 = "6001";
Integer integerEmpId9 = Integer.decode(empId6);
System.out.println(integerEmpId9);
 
Output :
6001

Using new Integer(string)

1
2
3
4
5
6
String empId7 = "7001";
Integer integerEmpId10 = new Integer(empId7);
System.out.println(integerEmpId10);
 
Output :
7001

However, remember that this Integer constructor is deprecated since Java9.

Complete Program 

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 decimal
   Integer.parseInt("1.1");
 
   //empty String
   Integer.parseInt("");
 
   //Blank space
   Integer.parseInt(" ");
 }
}

Dependencies used :

01
02
03
04
05
06
07
08
09
10
11
12
<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>

Summary

So, in this tutorial, we saw

– How we can convert String to int(or Integer) in Java.

– In most of the scenarios we can use Integer.parseInt() and Integer.valueOf(),if we want int or Integer

respectively from String and want to avoid dependency on third party libraries.
– NumberUtils. toInt() can be used in the scenarios ,where we don’t want our program to fail due NumberFormatException.

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: How to convert String to int in Java

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Gaurav Bhardwaj

Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Google Chorme
5 years ago

Hi…
I’m Elena gillbert.The Integer.parseInt() java method is used primarily in parsing a String method argument into an Integer object. The Integer object is a wrapper class for the int primitive data type of java API.Eventhough the conversion of String to Integer object is a basic stuff, I am suggesting that we should have a mastery on it.

Back to top button