{"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[pastacode lang=\u201djava\u201d manual=\u201dString%20numberAsString%20%3D%20%E2%80%9C5678%22%3B%0A%0Aint%20number%20%3D%20Integer.parseInt(numberAsString)%3B%0A%0ASystem.out.println(%22The%20number%20is%3A%20%22%20%2B%20number)%3B\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201djava\u201d manual=\u201dString%20number%20As%20String%20%3D%20%E2%80%9C5678%22%3B%0A%0Aint%20number%20%3D%20Integer.valueOf(numberAsString)%3B%0A%0ASystem.out.println(%22The%20number%20is%3A%20%22%20%2B%20number)%3B\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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\u2019s intValue() method.<\/p>\n<p><b>Example <\/b><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dString%20number%20As%20String%20%3D%20%225678%22%3B%0A%0AInteger%20intObject%20%3D%20new%20Integer(numberAsString)%3B%0A%0Aint%20number%20%3D%20intObject.intValue()%3B\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><b>We can shorten to<\/b>:<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dString%20number%20As%20String%20%3D%20%225678%22%3B%0A%0Aint%20number%20%3D%20new%20Integer(numberAsString).intValue()%3B\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><b>or just<\/b>:<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dint%20number%20%3D%20new%20Integer(%225678%22).intValue()%3B\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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\u2019s String representation or it can parse a String into it\u2019s numerical representation.<\/p>\n<p><b>Example <\/b><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dString%20numberAsString%20%3D%20%225678%22%3B%0ADecimalFormat%20decimalFormat%20%3D%20new%20DecimalFormat(%22%23%22)%3B%0Atry%20%0A%7B%0A%20%20%20int%20number%20%3D%20decimalFormat.parse(numberAsString).intValue()%3B%0A%20%20%20System.out.println(%22The%20number%20is%3A%20%22%20%2B%20number)%3B%0A%7D%0A%20catch%20(ParseException%20e)%20%0A%7B%0A%20%20%20System.out.println(numberAsString%20%2B%20%22%20is%20not%20a%20valid%20number.%22)%3B%0A%7D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201djava\u201d manual=\u201dBinary%20%0AString%20numberAsString%20%3D%20%2211111111%22%3B%0Aint%20wikinumber1%20%3D%20Integer.valueOf(numberAsString%2C%202)%3B%0Aint%20wikinumber2%20%3D%20Integer.parseInt(numberAsString%2C%202)%3B%0ASystem.out.println(wikinumber1)%3B%0ASystem.out.println(wikinumber2)%3B%20%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><b>output: <\/b><\/p>\n<p>255<\/p>\n<p>255<\/p>\n<p><b>Octal <\/b><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dString%20numberAsString%20%3D%20%22377%22%3B%0Aint%20wikinumber1%20%3D%20Integer.valueOf(numberAsString%2C%208)%3B%0Aint%20wikinumber2%20%3D%20Integer.parseInt(numberAsString%2C%208)%3B%0ASystem.out.println(wikinumber1)%3B%0ASystem.out.println(wikinumber2)%3B%20%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><b>output: <\/b><\/p>\n<p>255<\/p>\n<p>255<\/p>\n<p><b>Hexadecimal <\/b><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dString%20numberAsString%20%3D%20%22ff%22%3B%0Aint%20wikinumber1%20%3D%20Integer.valueOf(numberAsString%2C%2016)%3B%0Aint%20wikinumber2%20%3D%20Integer.parseInt(numberAsString%2C%2016)%3B%0ASystem.out.println(wikinumber1)%3B%0ASystem.out.println(wikinumber2)%3B%20%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}