Title: String to Int Java Example (with video) - Examples Java Code Geeks
Open Graph Title: String to Int Java Example (with video) - Examples Java Code Geeks
Description: Check out our example on how to convert in Java a String to Int. We will also see examples of the parseInt and the valueOf Java methods.
Open Graph Description: Check out our example on how to convert in Java a String to Int. We will also see examples of the parseInt and the valueOf Java methods.
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/string-to-int-java-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/string-to-int-java-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/string-to-int-java-example/"},"author":{"name":"Katerina Zamani","@id":"https://examples.javacodegeeks.com/#/schema/person/94be714b6b9a82bb855c0d370c319673"},"headline":"String to Int Java Example (with video)","datePublished":"2014-01-20T10:35:50+00:00","dateModified":"2022-07-06T10:35:13+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/string-to-int-java-example/"},"wordCount":430,"commentCount":1,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/string-to-int-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","keywords":["Integer"],"articleSection":["String"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/string-to-int-java-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/string-to-int-java-example/","url":"https://examples.javacodegeeks.com/string-to-int-java-example/","name":"String to Int Java Example (with video) - Examples Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/string-to-int-java-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/string-to-int-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-01-20T10:35:50+00:00","dateModified":"2022-07-06T10:35:13+00:00","description":"Check out our example on how to convert in Java a String to Int. We will also see examples of the parseInt and the valueOf Java methods.","breadcrumb":{"@id":"https://examples.javacodegeeks.com/string-to-int-java-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/string-to-int-java-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/string-to-int-java-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/string-to-int-java-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"String","item":"https://examples.javacodegeeks.com/category/java-development/core-java/string-core-java/"},{"@type":"ListItem","position":5,"name":"String to Int Java Example (with video)"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/94be714b6b9a82bb855c0d370c319673","name":"Katerina Zamani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/11/Katerina-Zamani-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/11/Katerina-Zamani-96x96.jpg","caption":"Katerina Zamani"},"description":"Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.","sameAs":["https://www.javacodegeeks.com"],"url":"https://examples.javacodegeeks.com/author/katerina-zamani/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"String","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/string-core-java\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-01-20T12:35:50+02:00","datePublished":"2014-01-20T12:35:50+02:00","dateModified":"2022-07-06T13:35:13+03:00","headline":"String to Int Java Example (with video)","name":"String to Int Java Example (with video)","keywords":"Integer","url":"https:\/\/examples.javacodegeeks.com\/string-to-int-java-example\/","description":"In this post, we feature a comprehensive article on how to convert in Java a String to Int. We will also see examples of the parseInt and the valueOf Java methods. String to Integer conversion is a fr","copyrightYear":"2014","articleSection":"String","articleBody":"\nIn this post, we feature a comprehensive article on how to convert in Java a String to Int. We will also see examples of the parseInt and the valueOf Java methods.\n\n\n\nString to Integer conversion is a frequently used procedure because String and Integer are very common data types.\n\n\n\nIn this example, we are going to show two different ways of converting String to Integer in Java, by using parseInt() and valueOf() methods.\n\n\n\nYou can also check this tutorial in the following video:\n\n\n\nString to int and int to String in Java - Video\n\n\n\n1. Syntax of parseInt() in Java\n\n\n\nThere are two different expressions for the parseInt() Java method:\n\n\n\npublic static int parseInt(String s, int radix)public static int parseInt(String s)\n\n\n\nThe parameters are:\n\n\n\ns: is the string, that includes the integer representation we want to parse.radix: is optional and it represents the base of the used numbering system while parsing s.\n\n\n\nBoth expressions return the integer value represented by the string argument. If radix parameter is used, the returned value is expressed by the specified radix. Otherwise the default radix is set to 10, for decimal system.\n\n\n\n2. Syntax of valueOf() in Java\n\n\n\nvalueOf() method, which is utilized for string to int conversion too, can be expressed in two ways:\n\n\n\npublic static Integer valueOf(String s, int radix).public static Integer valueOf(String s).\n\n\n\nThe parameters of valueOf() method are:\n\n\n\ns: represents the parsing string.radix: is the radix that is used in the parsing string. This parameter is optional.\n\n\n\nThe returned value of valueOf() method is an Integer object that represents the string argument. If the radix argument is used, the representation will be expressed by the specific radix. Otherwise, the value is a signed decimal integer.\n\n\n\n3. Example of String to Integer Java conversion\n\n\n\nCreate a java class with the name StringToIntTest.java and paste the following code.\n\n\n\nStringToIntTest.java:\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n\n\n\npackage com.javacodegeeks.javabasics.stringtoint;\n \npublic class StringToIntTest {\n \n public static void main(String args[]) {\n \n String testString1 = \"123456789\";\n String testString2 = \"00123\";\n String errorString = \"0.0123\";\n \n try{\n \/\/ using Integer.parseInt\n \/\/ with radix\n int int1 = Integer.parseInt(testString1, 10);\n \/\/ default radix = 10\n int int2 = Integer.parseInt(testString2);\n System.out.println(\"With parseInt method, int1 = \" + int1 + \" and int2 = \"+int2);\n \n \/\/ error situation\n int interror = Integer.parseInt(errorString);\n System.out.println(\"With parseInt method, interror =\" + interror);\n }catch(NumberFormatException ex) {\n System.err.println(\"NumberFormatException in parseInt, \"+ ex.getMessage());\n }\n \n try{\n \/\/ using Integer.valueOf without radix\n int int1 = Integer.valueOf(testString1);\n int int2 = Integer.valueOf(testString2);\n System.out.println(\"With valueOf method, int1 = \" + int1 + \" and int2 = \" + int2);\n \n \/\/ error situation\n int interror = Integer.valueOf(errorString);\n System.out.println(\"With valueOf method, interror =\" + interror);\n }catch(NumberFormatException ex) {\n System.err.println(\"NumberFormatException in valueOf, \"+ ex.getMessage());\n }\n \n }\n \n}\n\n\n\n\n\n\n\n\n\n\nIn the code above, we use both ways in order to convert a string to int and we take some different conditions. Notice that testString2 string contains two leading zeros. As you can see in the output below, both methods will ignore these zeros at the beginning and will display the integer. Also notice that we use NumberFormatException if the string cannot be parsed as an integer.\n\n\n\nYou can see the output of the execution below:\n\n\n\nOutput\n\n\n\nWith parseInt method, int1 = 123456789 and int2 = 123\nNumberFormatException in parseInt, For input string: \"0.0123\"\nWith valueOf method, int1 = 123456789 and int2 = 123\nNumberFormatException in valueOf, For input string: \"0.0123\"\n\n\n\n\nAs you can see it throws a NumberFormatException for the errorString, as it couldn't be parsed to integer. Also, the leading zeros are ignored, as we said before.\n\n\n\n4. More articles\n\n\n\nConvert int to string Java ExampleJava String Class ExampleString CompareTo Java Example\n\n\n\n5. Download the source code\n\n\n\nThis was an example of string to integer Java conversion. \n\n\n\nDownload\nDownload the source code of this example: String to Int Java Example\n\n\n\nLast updated on May 13th, 2021\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/string-to-int-java-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Katerina Zamani","url":"https:\/\/examples.javacodegeeks.com\/author\/katerina-zamani\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-01-20T10:35:50+00:00 |
| article:modified_time | 2022-07-06T10:35:13+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Katerina Zamani |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Katerina Zamani |
| twitter:label2 | Est. reading time |
| twitter:data2 | 4 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1