How to convert a String to an Int in Java?
Given String str containing digits as characters, the task is to convert this given string to integer in Java.
Examples:
Input: str = "1234" Output: 1234 Input: str = "213s" Output: 0 Since the String contains other than digits, hence the integer value will be 0
- Method 1: Use Integer.parseInt() method
This is the most simple method to convert String to integer. This function parses the string argument as a signed decimal integer.
Syntax:
public static int parseInt(String s) throws NumberFormatExceptionBelow is the implementation of the above approach:
// Java program to convert String to int// using Integer.parseInt() methodimportjava.io.*;classGFG {// Function to convert String to integerpublicstaticintconvert(String str){intval =0;System.out.println("String = "+ str);// Convert the Stringtry{val = Integer.parseInt(str);}catch(NumberFormatException e) {// This is thrown when the String// contains characters other than digitsSystem.out.println("Invalid String");}returnval;}// Driver codepublicstaticvoidmain(String[] args){String str ="1234";intval = convert(str);System.out.println("Integer value = "+ val);System.out.println();str ="123s";val = convert(str);System.out.println("Integer value = "+ val);}}chevron_rightfilter_noneOutput:String = 1234 Integer value = 1234 String = 123s Invalid String Integer value = 0
- Method 2: Use Ints::tryParse method of Guava library
Another method to convert String to integer is to use Ints::tryParse method of Guava library. It is similar to the Integer.parseInt() method, but this method is more concise and powerful.Syntax:
public static Integer tryParse(String s)
Below is the implementation of the above approach:
// Java program to convert String to int// using Ints::tryParse methodimportjava.io.*;importjava.util.*;importcom.google.common.primitives.Ints;classGFG {// Function to convert String to integerpublicstaticintconvert(String str){intval =0;System.out.println("String = "+ str);// Convert the Stringval = Optional.ofNullable(str).map(Ints::tryParse).orElse(0);returnval;}// Driver codepublicstaticvoidmain(String[] args){String str ="1234";intval = convert(str);System.out.println("Integer value = "+ val);System.out.println();str ="123s";val = convert(str);System.out.println("Integer value = "+ val);}}chevron_rightfilter_noneOutput:String = 1234 Integer value = 1234 String = 123s Integer value = 0
Recommended Posts:
- Convert a List of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert a Set of String to a comma separated String in Java
- Convert Set of String to Array of String in Java
- Convert String to Date in Java
- How to convert an Array to String in Java?
- Convert String to Double in Java
- How to Convert a Double value to String value in Java with Examples
- How to Convert a String value to Byte value in Java with Examples
- How to Convert a String value to Float value in Java with Examples
- How to Convert a Byte value to String value in Java with Examples
- Program to convert IntStream to String in Java
- Program to Convert Set of Integer to Set of String in Java
- Program to convert String to IntStream in Java
- Program to convert set of String to set of Integer in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

