{"id":21797,"date":"2017-04-02T15:35:46","date_gmt":"2017-04-02T15:35:46","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=21797"},"modified":"2024-11-16T14:39:24","modified_gmt":"2024-11-16T14:39:24","slug":"how-to-convert-string-to-integer-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-convert-string-to-integer-in-java\/","title":{"rendered":"How to convert String to Integer in Java?"},"content":{"rendered":"<h1>Overview<\/h1>\n<p>In this post, we will answer the question of How to convert String to Integer in Java.<\/p>\n<p>We can use Integer.valueOf() or the Integer.parseInt() methods to convert the string to integer. Both the methods may throw <em>java.lang.NumberFormatException\u00a0<\/em>exception. We can add the conversion in the <em>try-catch<\/em> block to handle the exception.<\/p>\n<h2>Integer.valueOf()<\/h2>\n<p>We can use this method to convert the String object to an Integer object.<\/p>\n<p><em>public static Integer valueOf(String s) throws NumberFormatException {<\/em><\/p>\n<p><em>&#8230;<\/em><\/p>\n<p><em>}<\/em><\/p>\n<h2>Integer.parseInt()<\/h2>\n<p>We can use this method to convert the String object to a primitive <em>int<\/em> data type.<\/p>\n<p><em>public static int parseInt(String s, int radix)<\/em><br \/>\n<em>throws NumberFormatException<\/em><br \/>\n<em>{<\/em><\/p>\n<p><em>&#8230;<\/em><\/p>\n<p><em>}<\/em><\/p>\n<h2>Using Integer constructor<\/h2>\n<p>Integer iObj = new Integer(str);<\/p>\n<p>Note that the above conversion using the Integer class constructor is deprecated since Java 9. It&#8217;s recommended to not use this constructor with Java 9 or above.<\/p>\n<h2>Java Program<\/h2>\n<p>Sample Java Program to convert Java String object to Integer object.<\/p>\n<pre>public class StringToInteger {\r\n\r\n\tpublic static void main(String args[]){\r\n\r\n\t\t\/\/Create New String object\r\n\t\tString str = new String(\"79\");\r\n\r\n\t\t\/\/Using valueOf(String) method \r\n\t\tInteger iObj = Integer.valueOf(str); \r\n\t\t\/\/Using parseInt(String) method \r\n\t\tint i = Integer.parseInt(str);\r\n\t\tSystem.out.println(\"String converted to Integer Object: \" + iObj);\r\n\t\tSystem.out.println(\"String converted to primitive int: \" + i);\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-21809\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Convert-String-to-Integer.png\" alt=\"Convert String to Integer\" width=\"1920\" height=\"1070\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Convert-String-to-Integer.png 1920w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Convert-String-to-Integer-300x167.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Convert-String-to-Integer-1024x571.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Convert-String-to-Integer-768x428.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Convert-String-to-Integer-1536x856.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/p>\n<p>In this program, we will show the usage of both the methods discussed above.<\/p>\n<p><em>String str = new String(&#8220;79&#8221;);<\/em><\/p>\n<p>Create a String object <em>str<\/em>.<\/p>\n<p><em>Integer iObj = Integer.valueOf(str);<\/em><\/p>\n<p>Convert the String object to an Integer object.<\/p>\n<p>int i = Integer.parseInt(str);<\/p>\n<p>Convert the String to primitive datatype <em>int<\/em>.<\/p>\n<p>&#8212;<\/p>\n<h2>Java Tutorials<\/h2>\n<p>Java Tutorial on this website:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview In this post, we will answer the question of How to convert String to Integer in Java. We can use Integer.valueOf() or the Integer.parseInt() methods to convert the string to integer. Both the methods may throw java.lang.NumberFormatException\u00a0exception. We can add the conversion in the try-catch block to handle the exception. Integer.valueOf() We can use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-21797","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=21797"}],"version-history":[{"count":18,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21797\/revisions"}],"predecessor-version":[{"id":23193,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21797\/revisions\/23193"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=21797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=21797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=21797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}