Get all updates of Java4s
Follow Us: Email Newsletter

How to Convert String to int in Java

Core Java » On Oct 15, 2016 By Sivateja

In Java we can convert string to integer with Integer.parseInt() function.

Java String to int Example

Output:
4

 

Note:

  • parseInt() will convert string “4” to integer
  • In order to use Integer.parseInt() method, we should pass string having numbers only. If input string contains other than numbers parseInt() function will throw java.lang.NumberFormatException. Would you like to see this scenario with an example ? :-)

Output:
Exception in thread “main” java.lang.NumberFormatException: For input string: “4s”
                     at java.lang.NumberFormatException.forInputString(Unknown Source)
                    at java.lang.Integer.parseInt(Unknown Source)
                    at java.lang.Integer.parseInt(Unknown Source)
                    at StringtoInt.main(StringtoInt.java:10)

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.