How to convert numeric String to int in Java?

Asked 27-Feb-2018
Viewed 317 times

1 Answer


0

For converting a numeric string to an int using the parseInt method of the Java Integer class. The parseInt method converts the String to an int, and throws a NumberFormatException if the string can’t be converted to an int type.
Example for converting numeric string to int in java -

public class StringToIntExample
{
  public static void main (String[] args)
  {
      String s = "100"; // Numeric String
    try
    {

      int i = Integer.parseInt(s.trim());
      System.out.println("int i = " + i);
    }
    catch (NumberFormatException e)
    {
      System.out.println("NumberFormatException: " + e.getMessage());
    }
  }
}