DEV Community

Tyler Rodriguez
Tyler Rodriguez

Posted on

Integer.parseInt() will only work with Strings that are...well...ints.

String numString = "8.35";
Integer numInt = Integer.parseInt(numString);

THIS WILL NOT WORK!!!

I made the (wrong) assumption that parseInt will just convert 8.35 to an Integer value of 8 and then do the parsing. NO! WRONG! It will throw an exception. I forgot which exactly. I will add it in later when I check. You can update blogs on dev.to, right? I hope so. Anyways, that's what I have learned. So I thought I would write this post to share my knowledge.

Top comments (2)

Collapse
 
vishwaratna profile image
Vishwa Ratna

The exception thrown by Integer.parseInt() is NumberFormatException.

Integer.valueOf() takes both integer and string and and returns a Integer Object,While parseInt() will return primitive int and it takes only string as argument.

Collapse
 
pyguyty profile image
Tyler Rodriguez

Thank you!