How to convert a String to an int in Java

In this section, we will show you how to convert a String to an int in Java. 

Input: str = "3456" 

Output: 3456


Input: str = "a3456e" 

Output: 0


Input: str = "-3456" 

Output: 3456

Following ways can be used for converting String to int:

1. By using Integer.parseInt() method

2. By using NumberUtils.toInt method of Apache Commons library

3. By using Ints::tryParse method of Guava library


Example 1: Use Integer.parseInt() method

Integer.parseInt() method parses the String argument as a signed decimal integer object. The characters in the string must be decimal digits, except that the first character of the string may be an ASCII minus sign '-' to indicate a negative value or an ASCII plus '+' sign to indicate a positive value. It returns the integer value which is represented by the argument in a decimal integer.

public class Main {

// Driver code
public static void main(String[] args)
{

//Example 1
String str1 = "3456";
int value1 = convertToInt(str1);
System.out.println("Integer value = " + value1);

System.out.println();

//Example 2
String str2 = "a3456e";
int value2 = convertToInt(str2);
System.out.println("Integer value = " + value2);

System.out.println();

//Example 3
String str3 = "-3456";
int value3 = convertToInt(str3);
System.out.println("Integer value = " + value3);

System.out.println();

//Example 4
String str4 = "+3456";
int value4 = convertToInt(str4);
System.out.println("Integer value = " + value4);

}

// Method to convert String to int
public static int convertToInt(String str)
{
int value = 0;
System.out.println("String = " + str);

// Convert the String
try {
value = Integer.parseInt(str);
}
catch (NumberFormatException e) {

/* This is thrown when the String
contains characters other than digits
*/
System.out.println("Invalid String");
}
return value;
}
}

Console Output:
String = 3456
Integer value = 3456

String = a3456e
Invalid String
Integer value = 0

String = -3456
Integer value = -3456

String = +3456
Integer value = 3456


Example 2: Use NumberUtils.toInt method of Apache Commons library

Download Apache Commons library:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

Another method to convert String to int is to use NumberUtils.toInt() method of Apache Commons library. If the string is an invalid number format, then 0 is always returned.

import org.apache.commons.lang3.math.NumberUtils;

public class Main {

// Driver code
public static void main(String[] args)
{

//Example 1
String str1 = "3456";
int value1 = convertToInt(str1);
System.out.println("Integer value = " + value1);

System.out.println();

//Example 2
String str2 = "a3456e";
int value2 = convertToInt(str2);
System.out.println("Integer value = " + value2);

System.out.println();

//Example 3
String str3 = "-3456";
int value3 = convertToInt(str3);
System.out.println("Integer value = " + value3);

}

// Method to convert String to int
public static int convertToInt(String str)
{
int value = 0;
System.out.println("String = " + str);

// Convert the String
value = NumberUtils.toInt(str);
return value;
}
}


Console Output:
String = 3456
Integer value = 3456

String = a3456e
Integer value = 0

String = -3456
Integer value = -3456


Example 3: Use Ints::tryParse method of Guava library

Download Guava library:

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>

Another method to convert String to integer is to use Ints::tryParse method of Guava library.

import com.google.common.primitives.Ints;
import java.util.Optional;

public class Main {

// Driver code
public static void main(String[] args)
{

//Example 1
String str1 = "3456";
int value1 = convertToInt(str1);
System.out.println("Integer value = " + value1);

System.out.println();

//Example 2
String str2 = "a3456e";
int value2 = convertToInt(str2);
System.out.println("Integer value = " + value2);

System.out.println();

//Example 3
String str3 = "-3456";
int value3 = convertToInt(str3);
System.out.println("Integer value = " + value3);

}

// Method to convert String to int
public static int convertToInt(String str)
{
int value = 0;
System.out.println("String = " + str);

// Convert the String
value = Optional.ofNullable(str)
.map(Ints::tryParse)
.orElse(0);
return value;
}
}


Console Output:
String = 3456
Integer value = 3456

String = a3456e
Integer value = 0

String = -3456
Integer value = -3456

More related topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete