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. There are many ways to convert a string to integer in java.

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);
   }
}

Method 2 : Using valueOf method
This is another method to parse a string to an int 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);
   }
}


Method 3
: Using java.lang.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);
   }
}

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.

Liked the article, click the clap now!!!

0
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.