How can I convert a String value to an int type?
"1234" → 1234
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you look at the Java documentation you’ll notice the "catch" is that this function can throw a
NumberFormatException, which you can handle:(This treatment defaults a malformed number to
0, but you can do something else if you like.)Alternatively, you can use an
Intsmethod from the Guava library, which in combination with Java 8’sOptional, makes for a powerful and concise way to convert a string into an int: