{"id":1066,"date":"2017-03-20T11:25:29","date_gmt":"2017-03-20T05:55:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1066"},"modified":"2017-03-29T12:19:37","modified_gmt":"2017-03-29T06:49:37","slug":"convert-string-int-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/convert-string-int-java\/","title":{"rendered":"JAVA &#8211; How to convert a String to an int in Java"},"content":{"rendered":"<h4 id=\"data-types-in-java\"><span style=\"color: #800000;\"><b>Data Types in Java: <\/b><\/span><\/h4>\n<p>Data types represent the different values to be stored in the variable. In java, there are two types of data types:<\/p>\n<ul>\n<li>Primitive data types<\/li>\n<li>Non-primitive data types<\/li>\n<\/ul>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-1068 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/ov-300x231.png\" alt=\"\" width=\"404\" height=\"311\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/ov-300x231.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/ov.png 612w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/p>\n<ul>\n<li>For the other primitive\u00a0<b>types<\/b>\u00a0(char, byte, short, int, long, float, and double), there are two kinds of\u00a0<b>conversion<\/b>: implicit and explicit.<\/li>\n<li>Implicit\u00a0<b>conversions<\/b>: An implicit <b>conversion<\/b>\u00a0means that a value of one\u00a0<b>type<\/b>\u00a0is changed to a value of another\u00a0<b>type <\/b>without any special directive from the programmer.<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><b>E<\/b><b>xample codes on how to convert\u00a0Java String To Int.<\/b><\/span><\/p>\n<ul>\n<li>Convert using Integer.parseInt()<\/li>\n<li>Convert using Integer.valueOf()<\/li>\n<li>Convert using new Integer(String).intValue()<\/li>\n<li>Convert using DecimalFormat<\/li>\n<li>Convert with special radix (not base 10 system)<\/li>\n<\/ul>\n<h4 id=\"convert-using-integer-parseint\"><span style=\"color: #ff6600;\"><b>Convert using Integer.parseInt()<\/b><\/span><\/h4>\n<p>The Integer.parseInt() static method parses the string argument as a signed decimal integer and returns an int value.<\/p>\n<p><b>Syntax <\/b><\/p>\n<p>public static int parseInt(String s) throws Number Format Exception<\/p>\n<p>The parameter <b>s <\/b>will be converted to a primitive int value. Note that the method will throw a NumberFormatException if the parameter is not a valid int.<\/p>\n<p><b>Example <\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">String numberAsString = \u201c5678&quot;;<br\/><br\/>int number = Integer.parseInt(numberAsString);<br\/><br\/>System.out.println(&quot;The number is: &quot; + number);<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>When you run the above code, <b>the output <\/b>will show this:<\/p>\n<p>The number is: 5678<\/p>\n<p><b>Note: <\/b>The resulting value is not an instance of the Integer class but just a plain primitive int value.<\/p>\n<h4 id=\"convert-using-integer-valueof\"><span style=\"color: #800080;\"><b>Convert using <\/b><b>Integer.valueOf<\/b><b>()<\/b><\/span><\/h4>\n<p>The Integer.valueOf() static method will return an Integer object holding the value of the specified String.<\/p>\n<p><b>Syntax <\/b><\/p>\n<p>public static Integer valueOf(String s) throws NumberFormatException<\/p>\n<p><b>Note: <\/b>The method will throw a NumberFormatException if the parameter is not a valid integer.<\/p>\n<p><b>Example<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">String number As String = \u201c5678&quot;;<br\/><br\/>int number = Integer.valueOf(numberAsString);<br\/><br\/>System.out.println(&quot;The number is: &quot; + number);<\/code><\/pre> <\/div>\n<p><b>This will output:<\/b><\/p>\n<p>The number is: 5678<\/p>\n<p><b>Note:<\/b> The resulting value is an instance of the Integer class and not a primitive int value.<\/p>\n<h4 id=\"convert-using-new-integerstring-intvalue\"><span style=\"color: #0000ff;\"><b>Convert using new Integer(String).intValue()<\/b><\/span><\/h4>\n<p>Another alternative method is to create an instance of Integer class and then invoke it&#8217;s intValue() method.<\/p>\n<p><b>Example <\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">String number As String = &quot;5678&quot;;<br\/><br\/>Integer intObject = new Integer(numberAsString);<br\/><br\/>int number = intObject.intValue();<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><b>We can shorten to<\/b>:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">String number As String = &quot;5678&quot;;<br\/><br\/>int number = new Integer(numberAsString).intValue();<\/code><\/pre> <\/div>\n<p><b>or just<\/b>:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">int number = new Integer(&quot;5678&quot;).intValue();<\/code><\/pre> <\/div>\n<h4 id=\"convert-using-decimal-format\"><span style=\"color: #800000;\"><b>Convert using Decimal Format<\/b><\/span><\/h4>\n<p>The class\u00a0<b>java.text.DecimalFormat<\/b>\u00a0is a class that can be used to convert a number to it&#8217;s String representation or it can parse a String into it&#8217;s numerical representation.<\/p>\n<p><b>Example <\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">String numberAsString = &quot;5678&quot;;<br\/>DecimalFormat decimalFormat = new DecimalFormat(&quot;#&quot;);<br\/>try <br\/>{<br\/>   int number = decimalFormat.parse(numberAsString).intValue();<br\/>   System.out.println(&quot;The number is: &quot; + number);<br\/>}<br\/> catch (ParseException e) <br\/>{<br\/>   System.out.println(numberAsString + &quot; is not a valid number.&quot;);<br\/>}<\/code><\/pre> <\/div>\n<p><b>Output <\/b><\/p>\n<p>The number is: 5678<\/p>\n<p><b>Note: <\/b>The parse() method will throw a ParseException when there are problems encountered with the String.<\/p>\n<h4 id=\"convert-with-special-radix\"><span style=\"color: #993366;\"><b>Convert with special radix<\/b><\/span><\/h4>\n<p>The above examples uses the base (radix) 10. There are cases when we wish to convert a Java String to Integer but using another base.<\/p>\n<p>Both Integer.parseInt() and Integer.valueOf() can receive a custom radix to be used in the conversion.<\/p>\n<p><b>Example<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">Binary <br\/>String numberAsString = &quot;11111111&quot;;<br\/>int wikinumber1 = Integer.valueOf(numberAsString, 2);<br\/>int wikinumber2 = Integer.parseInt(numberAsString, 2);<br\/>System.out.println(wikinumber1);<br\/>System.out.println(wikinumber2); <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><b>output: <\/b><\/p>\n<p>255<\/p>\n<p>255<\/p>\n<p><b>Octal <\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">String numberAsString = &quot;377&quot;;<br\/>int wikinumber1 = Integer.valueOf(numberAsString, 8);<br\/>int wikinumber2 = Integer.parseInt(numberAsString, 8);<br\/>System.out.println(wikinumber1);<br\/>System.out.println(wikinumber2); <\/code><\/pre> <\/div>\n<p><b>output: <\/b><\/p>\n<p>255<\/p>\n<p>255<\/p>\n<p><b>Hexadecimal <\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">String numberAsString = &quot;ff&quot;;<br\/>int wikinumber1 = Integer.valueOf(numberAsString, 16);<br\/>int wikinumber2 = Integer.parseInt(numberAsString, 16);<br\/>System.out.println(wikinumber1);<br\/>System.out.println(wikinumber2); <\/code><\/pre> <\/div>\n<p><b>output: <\/b><\/p>\n<p>255<\/p>\n<p>255<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data Types in Java: Data types represent the different values to be stored in the variable. In java, there are two types of data types: Primitive data types Non-primitive data types For the other primitive\u00a0types\u00a0(char, byte, short, int, long, float, and double), there are two kinds of\u00a0conversion: implicit and explicit. Implicit\u00a0conversions: An implicit conversion\u00a0means that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[2179,2164,2163,2166,2167,2169,2165,2176,2174,2172,2173,2175,2168,2171,2170,2178,2177],"class_list":["post-1066","post","type-post","status-publish","format-standard","hentry","category-java","tag-cast-string-to-int-java-android","tag-convert-string-to-int-c","tag-convert-string-to-int-in-c","tag-convert-string-to-int-javascript","tag-convert-string-to-int-php","tag-convert-string-to-int-swift","tag-convert-string-to-integer-python","tag-easiest-way-to-convert-int-to-string-in-c","tag-how-can-i-convert-string-to-int","tag-how-do-i-convert-a-string-to-an-inputstream-in-java","tag-how-to-convert-byte-to-string","tag-how-to-split-a-string-in-java","tag-java-convert-string-to-double","tag-parse-string-to-float-or-int","tag-readconvert-an-inputstream-to-a-string","tag-run-time-error-integer-convert-to-string","tag-why-is-it-faster-to-process-a-sorted-array-than-an-unsorted-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1066"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}