In this article, we will take a look at 3 ways to convert a string to an int in java with examples code and its explanation.


Method 1 : Using parseInt() method
java.lang.Integer class has a static parseInt()method which takes a String as an argument and returns a java.lang.Integer object.
The integer returned has a numeric value equal to the number represented by the String.

public class StringToIntConverter { 
   public static void main(String[] args) { 
      String str = "100"; 
      int i = Integer.parseInt(str); 
      System.out.println("Integer value is : " + i); 
   } 
}

This method throws a NumberFormatException, if the number in string format is not parseable into an int value, such as “abc”.
Method 2 : Using valueOf() method
This is another method of string to int conversion in java. This method converts a string to int without using parseInt() method.
java.lang.Integerclass has a static valueOf()method which takes a string as an argument and returns a java.lang.Integerobject.
The integer returned has a numeric value equal to the number represented by the String.

public class StringToIntConverter { 
   public static void main(String[] args) { 
      String str = "100"; 
      int i = Integer.valueOf(str); 
      System.out.println("Integer value is : " + i);
   } 
}

This method throws a NumberFormatException, if the number in string format is not parseable into an int value. For example, “ten”.
Method 3
: Using Integer constructor
java.lang.Integerclass has a constructor which takes a string as an argument and returns a java.lang.Integerobject.
The integer returned has a numeric value equal to the number represented by the String.
This method also converts a string to integer without using parseInt() method.

public class StringToIntConverter { 
   public static void main(String[] args) { 
      String s = "100"; 
      int i = new Integer(str); 
      System.out.println("Integer value is : " + i); 
   } 
}

Using Integer constructor has been deprecated since Java 9. Instead, valueOf() method should be used to convert a string to int.

Why convert string to integer

Suppose you have a web application which takes numeric items such as age, zip code, number of employees etc. as input from the user and store it in a database.
In ideal applications, database columns for such type of values would be of integer type and the values received from browser on the server would be in string format.
Thus a conversion from string to integer is necessary.

Let’s tweak in

  1. parseInt()method of java.lang.Integer class throws a java.lang.NumberFormatException if the String provided to it is not in number format.
    For Example, if the String is 100ab, then parseInt() will throw following error

    Exception in thread “main” java.lang.NumberFormatException: For input string: “100ab”

  2. Passing null to parseInt() method also throws a java.lang.NumberFormatException.
  3. java.lang.Integer can be assigned to an object of type java.lang.Integer or a primitive of type intas shown in above example.
    This is handled automatically by java and is called unboxing (conversion of a class to its corresponding primitive type).
  4. valueOf() method also throws a java.lang.NumberFormatException if the String passed to it is not in a numeric format.
  5. valueOf() method internally calls parseInt() method.

Hope the article was useful.