Program to Convert Java String to int
Let us take an example straight away to get used to the working parseInt() method :
Java
// Java program to demonstrate working parseInt() // Where No NumberFormatException is Thrown // Main class public class GFG { // Main driver method public static void main(String args[]) { // Custom wide-varied inputs to illustrate // usage of valueOf() method int decimalExample = Integer.parseInt( "20" ); int signedPositiveExample = Integer.parseInt( "+20" ); int signedNegativeExample = Integer.parseInt( "-20" ); int radixExample = Integer.parseInt( "20" , 16 ); int stringExample = Integer.parseInt( "geeks" , 29 ); // Print commands on console System.out.println(decimalExample); System.out.println(signedPositiveExample); System.out.println(signedNegativeExample); System.out.println(radixExample); System.out.println(stringExample); } } |
Output
20 20 -20 32 11670324
String to int in Java
In Java, while operating upon strings, there are times when we need to convert a number represented as a string into an integer type. We usually do this because we can operate with a wide flexible set of operations over strings. The method generally used to convert String to Integer in Java is parseInt() of the String class.
In this article, we will see how to convert a String to int in Java.