Convert String to int in Java

In Java, you can use Integer.parseInt() to convert a String to int.

1. Integer.parseInt() Examples

Example to convert a String “10” to an primitive int.



2. Integer.valueOf() Examples

Alternatively, you can use Integer.valueOf(), it will returns an Integer object.



3. NumberFormatException

If the string does not contain a parsable integer, a NumberFormatException will be thrown.


Exception in thread "main" java.lang.NumberFormatException: For input string: "10A"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)

Sushil Singh

My name is Sushil Kumar Singh. I’m the founder, designer and editor of this blog. I would love to hear your thoughts and opinions on my articles directly. You can write to me at I’ll try to respond as soon as possible. Alternatively, you can connect with me directly on linkedin or github or Facebook.

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of
%d bloggers like this: