Java – Convert String to int

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.


	String number = "10";
	int result = Integer.parseInt(number);			
	System.out.println(result);

Output


10

2. Integer.valueOf() Examples

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


	String number = "10";
	Integer result = Integer.valueOf(number);		
	System.out.println(result);

Output


10
Note
In summary, parseInt(String) returns a primitive int, whereas valueOf(String) returns a new Integer() object.

3. NumberFormatException

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


	String number = "10A";
	int result = Integer.parseInt(number);
	System.out.println(result);

Output


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)

References

  1. Integer.parseInt() JavaDoc
  2. Integer.valueOf() JavaDoc
  3. NumberFormatException JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
19 Comment threads
16 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
33 Comment authors
lovergirl69w4353herloSumita MaitykhalidIndra Recent comment authors
newest oldest most voted
anurag
Guest
anurag

Hello sir
this is well explained java String conversion.
really beautiful
thanks

Rahul Bavannavar
Guest
Rahul Bavannavar

I am getting still number format exception in httpservlet

Groot
Guest
Groot

I am GROOT!

ROOT
Guest
ROOT

Then I am ROOT
apt-get install GROOT
apt-get update

Stoneback
Guest
Stoneback

Kernal Panic

Groot
Guest
Groot

sudo dnf uninstall Groot
sudo dnf install Root version 1.0.2.Wrapper
sudo dnf uninstall Fedora 27
sudo dnf exit

prakash
Guest
prakash

ha ha ha…
u r from guardian of the galaxy

Groot
Guest
Groot

I am GROOT!

herlo
Guest
herlo

kk

Jettyjie
Guest
Jettyjie

why don’t use apache NumberUtils, or charge the String matchs d+,I think use Integer.valueof(String) directly is not good,we can avoid the exception

Dillip
Guest
Dillip

I am making a Movie Ticket System. User inputs seat number as “D13”. How to separate char row = ‘D’ and int column = 13?

Antonio
Guest
Antonio

Make a Object for this…

Elle
Guest
Elle

You could use substring to separate your row from the column

phaneesh
Guest
phaneesh

super

nnnn
Guest
nnnn

hhhh

Hey
Guest
Hey

test jj

Krishnendu Dutta
Guest
Krishnendu Dutta

thnx

Sumita Maity
Guest
Sumita Maity

if the string has trailing zeroes then the zeroes are getting truncated in java 8 , can you help ? I need to convert string 100100 to int 100100

lovergirl69w4353
Guest
lovergirl69w4353

kaate nehi kaate te din ye raat kehni thi tujhse so jo bil ki baat..looo aaj mein kheti hoon, I LOVE YOU

khalid
Guest
khalid

tanks

Indra
Guest
Indra

Thanks for your information. it’s so helpfull

Ashish
Guest
Ashish

Thanks for this explanation.

Ashish
Guest
Ashish

Thank you

John
Guest
John

If my string has more than 10 digits,the compiler gives me an error,there’s a way to fix it? How can I read more than that from a string?

Robert Miielewczyk
Guest
Robert Miielewczyk

max 32 bit integer is 2,147,483,647 which is why it gives you an error you cant assign such a big number, you should assign it to 64 bit int, or leave it as a String

hasib
Guest
hasib

how can i assign it into 64 bit

Passat
Guest
Passat

use long?

Chris Butterfield
Guest
Chris Butterfield

Use type long

gfdf
Guest
gfdf

no way

Hasbi, Muh
Guest
Hasbi, Muh

Dear Sir MKYong,
Thank you for your simple but precise explanation.
Regards.

bhargav
Guest
bhargav

I really felt so happy to this good things from MKYong site. I’m very happy now please do the need full as much has possible

Devendra Gaud
Guest
Devendra Gaud

thanks you

Makhdoom
Guest
Makhdoom

what if I want 1 and 0 separately.

kisna
Guest
kisna

super

Mena
Guest
Mena

really nice page dude :)
keep going !