{"id":4239,"date":"2017-11-04T21:50:07","date_gmt":"2017-11-04T16:20:07","guid":{"rendered":"https:\/\/www.java2blog.com\/?p=4239"},"modified":"2023-11-25T10:55:31","modified_gmt":"2023-11-25T05:25:31","slug":"java-convert-string-to-int","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-convert-string-to-int\/","title":{"rendered":"How to convert String to int in Java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Conversion_Modes\">Conversion Modes<\/a><\/li><li><a href=\"#parseIntString_method\">parseInt(String) method<\/a><ul><li><a href=\"#What_if_String_is_not_convertible_to_int\">What if String is not convertible to int<\/a><\/li><\/ul><\/li><li><a href=\"#Using_valueOfString_method\">Using valueOf(String) method<\/a><\/li><li><a href=\"#Using_the_Integer_class_constructor\">Using the Integer class constructor<\/a><\/li><\/ul><\/div>\n<p>In this article, we are going to see how we can convert from a String data type into integer data type in Java.<\/p>\n<h2><span id=\"Conversion_Modes\">Conversion Modes<\/span><\/h2>\n<p>There are two ways in which String data can be converted into integer. They are:<\/p>\n<ul>\n<li>Using the static method parseInt(String) of the java.lang.Integer wrapper class<\/li>\n<li>Using the static method valueOf(String) of the java.lang.Integer wrapper class<\/li>\n<li>Using the constructor of Integer wrapper class that takes String as its parameter<\/li>\n<\/ul>\n<p>Let us see both the modes and how to do them.<\/p>\n<h2><span id=\"parseIntString_method\"><span style=\"color: #f89820;\">parseInt(String) method<\/span><\/span><\/h2>\n<p>The class java.lang.Integer has a static method called parseInt(String) that allows the programmer to easily convert a String containing integer type data(i.e. numbers with no decimal points or post decimal values) into the corresponding Integer value. The function returns a value of primitive \u2018int\u2019 type.<\/p>\n<p>Let us see a code snippet to illustrate the same:<\/p>\n<pre class=\"java\" name=\"code\">package org.arpit.java2blog.java8;\n\npublic class ConvertStringToInteger {\n\n    public static void main(String[] args) {\n\n        String string = \"45\";\n        Integer stringtoInteger = Integer.parseInt(string);\n        int stringInt=stringtoInteger.intValue();\n        System.out.println(stringInt);\n    }\n}\n<\/pre>\n<p>The above code on execution provides the following output:<\/p>\n<div class=\"content-box-green\">45<\/div>\n<h3><span id=\"What_if_String_is_not_convertible_to_int\"><span style=\"color: #000080;\">What if String is not convertible to int<\/span><\/span><\/h3>\n<p>If String is not convertible to int, you will get below exception.<\/p>\n<div class=\"content-box-green\">Exception in thread &#8220;main&#8221; java.lang.NumberFormatException: For input string: &#8220;45.1&#8221;<br \/>\nat java.base\/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)<br \/>\nat java.base\/java.lang.Integer.parseInt(Integer.java:652)<br \/>\nat java.base\/java.lang.Integer.valueOf(Integer.java:983)<br \/>\nat org.arpit.java2blog.java8.ConvertStringToInteger.main(ConvertStringToInteger.java:8)<\/div>\n<h2><span id=\"Using_valueOfString_method\"><span style=\"color: #f89820;\">Using valueOf(String) method<\/span><\/span><\/h2>\n<p>You can also use valueOf(String) method to convert String to int in java. You can call Integer&#8217;s intValue() to convert it to primitive type.<\/p>\n<pre class=\"java\" name=\"code\">package org.arpit.java2blog.java8;\n\npublic class ConvertStringToInteger {\n\n    public static void main(String[] args) {\n\n        String string = \"45\";\n        Integer stringToInteger = Integer.valueOf(string);\n        int stringInt=stringToInteger.intValue();\n        System.out.println(stringInt);\n    }\n}\n<\/pre>\n<p>The above code on execution provides the following output:<\/p>\n<div class=\"content-box-green\">45<\/div>\n<h2><span id=\"Using_the_Integer_class_constructor\"><span style=\"color: #f89820;\">Using the Integer class constructor<\/span><\/span><\/h2>\n<p>The other method to convert to Integer is by using the parameterized constructor of java.lang.Integer class that accepts a String as a parameter input. This creates an object of the Integer class that stores the value of the String data sent as Integer, and can be accessed using its intValue() function or by accessing the wrapper class object itself.<\/p>\n<p>Let us see the code snippet:<\/p>\n<pre class=\"java\" name=\"code\">package org.arpit.java2blog.java8;\n\npublic class ConvertStringToInteger {\n\n    public static void main(String[] args) {\n\n        String string = \"45\";\n        Integer stringToInteger = new Integer(string);\n        int stringInt=stringToInteger.intValue();\n        System.out.println(stringInt);\n    }\n}\n<\/pre>\n<p>The above code on execution provides the following output:<\/p>\n<div class=\"content-box-green\">45<\/div>\n<p>That\u2019s all about String to Integer conversion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsConversion ModesparseInt(String) methodWhat if String is not convertible to intUsing valueOf(String) methodUsing the Integer class constructor In this article, we are going to see how we can convert from a String data type into integer data type in Java. Conversion Modes There are two ways in which String data can be converted into [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[183,27],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4239"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=4239"}],"version-history":[{"count":2,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4239\/revisions"}],"predecessor-version":[{"id":25948,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4239\/revisions\/25948"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=4239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=4239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=4239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}