{"id":235,"date":"2015-06-05T16:01:46","date_gmt":"2015-06-05T06:01:46","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=235"},"modified":"2015-11-20T18:35:16","modified_gmt":"2015-11-20T07:35:16","slug":"check-java-string-integer","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/check-java-string-integer\/","title":{"rendered":"How to check if a Java String is an integer?"},"content":{"rendered":"

The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.<\/p>\n

\r\npackage com.learnjava.string;\r\n\r\npublic class IntegerCheck {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tString[] input = {\r\n\t\t\t\t"abc",\r\n\t\t\t\t"9",\r\n\t\t\t\t"352",\r\n\t\t\t\t"5g",\r\n\t\t\t\t"-86",\r\n\t\t\t\t"6.7"\r\n\t\t\t};\r\n\r\n\t\t\t\/\/ Loop through an array of Strings,\r\n\t\t\t\/\/ testing if each is a integer or not\r\n\r\n\t\t\tfor (int i=0; i<input.length; i++) {\r\n\r\n\t\t\t\t\/\/ Test if next string is a integer\r\n\r\n\t\t\t\tboolean isInteger = isInteger(input[i]);\r\n\t\t\t\tif (isInteger) {\r\n\t\t\t\t\tSystem.out.println(input[i]+" is an integer");\r\n\t\t\t\t} else {\r\n\t\t\t\t\tSystem.out.println(input[i]+" is not an integer");\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t}\r\n\r\n\tpublic static boolean isInteger(String s) {\r\n\t\tboolean isValidInteger = false;\r\n\t\ttry\r\n\t\t{\r\n\t\t   Integer.parseInt(s);\r\n\r\n\t\t   \/\/ s is a valid integer\r\n\r\n\t\t   isValidInteger = true;\r\n\t\t}\r\n\t\tcatch (NumberFormatException ex)\r\n\t\t{\r\n\t\t   \/\/ s is not an integer\r\n\t\t}\r\n\r\n\t\treturn isValidInteger;\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4,89],"tags":[64,65,7],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-3N","jetpack-related-posts":[{"id":222,"url":"https:\/\/learn-java-by-example.com\/java\/list-array\/","url_meta":{"origin":235,"position":0},"title":"Using a List instead of an array","date":"March 8, 2012","format":false,"excerpt":"Lists and arrays can both be used to store ordered collections of data. Both have their strengths and weakness which we shall discuss in a later post. Previously we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":235,"position":1},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":235,"position":2},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":43,"url":"https:\/\/learn-java-by-example.com\/java\/palindromes\/","url_meta":{"origin":235,"position":3},"title":"Palindromes","date":"April 13, 2011","format":false,"excerpt":"A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":182,"url":"https:\/\/learn-java-by-example.com\/java\/high-guessing-game\/","url_meta":{"origin":235,"position":4},"title":"High Low Guessing Game","date":"May 17, 2011","format":false,"excerpt":"The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number. First thing we need\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":375,"url":"https:\/\/learn-java-by-example.com\/java\/prompt-user-console-input-validate-response\/","url_meta":{"origin":235,"position":5},"title":"Prompt user for console input and validate their response","date":"December 6, 2015","format":false,"excerpt":"We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used. It reads the input from the user and checks if it is a valid double. If its not it prompts the user\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":2,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":237,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/235\/revisions\/237"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}