In this tutorial we are going to see how to convert Java string to int.

Java String to int :

We can convert string to int in java using Integer Wrapper class. The Integer class given two different methods to convert String to int.

  • Integer.parseInt(“”);
  • Integer.valueOf(“”);

The above both methods takes String as parameter and return corresponding Integer value.

Note : Both methods will throw NumberFormatException, when an input string contains other then numbers.

String to int Integer.parseInt :

[java]

public class StringToInt {
    public static void main(String[] args) {
        String str = "15623";
        int intVal = Integer.parseInt(str);
        System.out.println(intVal);
    }
}

[/java]

[box type=”success” align=”alignleft” class=”” width=”100%”]
1526
[/box

String to int Integer.valueOf:

[java]

public class StringToInt {
    public static void main(String[] args) {
        String str = "15623";
        int intVal = Integer.valueOf(str);
        System.out.println(intVal);
    }
}
[/java]

[box type=”success” align=”alignleft” class=”” width=”100%”]
1526
[/box]

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment