Java

Java String to int conversion Example

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 :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment