|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Java Program To Check If the given String Is A Number or Not" |
| 4 | +author: gaurav |
| 5 | +categories: [Java Programs, Core Java] |
| 6 | +toc: true |
| 7 | +description: "In this tutorial, we will see a Java program to check if the given String is number or not." |
| 8 | +--- |
| 9 | + |
| 10 | +In this tutorial, we will see a Java program to check if the given String is number or not. |
| 11 | + |
| 12 | +We can check it using the regex expression. |
| 13 | + |
| 14 | +## Java Program To Check If the Given String is Number or Not Using Regex Expression |
| 15 | + |
| 16 | +In the below program we have simply used the regex expression to check whether the given string is numeric or not. |
| 17 | + |
| 18 | +The regex expression to check this is `-?\\d+` |
| 19 | + |
| 20 | +Here, |
| 21 | + |
| 22 | +`-?` --> negative sign, could have none or one |
| 23 | + |
| 24 | +`\\d+` --> one or more digits |
| 25 | + |
| 26 | +**Java Program** |
| 27 | + |
| 28 | +```java |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +/** |
| 32 | + * A Java program to check if the given String is number of not. |
| 33 | + * @author coderolls.com |
| 34 | + */ |
| 35 | +public class CheckIfNumber { |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + List<String> strings = List.of("95965", "Jack485", "-9859", "784Will", "056", "Google"); |
| 39 | + for(String str : strings) { |
| 40 | + boolean isNumber = isNumberOrNot(str); |
| 41 | + if (isNumber) { |
| 42 | + System.out.println("The String '" + str + "' is a Number."); |
| 43 | + } else { |
| 44 | + System.out.println("The String '" + str + "' is not a Number."); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * A method check if the input param string is a numeric or not |
| 52 | + * it is uses a regex Expression |
| 53 | + * -? --> negative sign, could have none or one |
| 54 | + * \\d+ --> one or more digits |
| 55 | + * @param str |
| 56 | + * @return |
| 57 | + */ |
| 58 | + private static boolean isNumberOrNot(String str) { |
| 59 | + return str.matches("-?\\d+"); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Output |
| 65 | + |
| 66 | +``` |
| 67 | +The String '95965' is a Number. |
| 68 | +The String 'Jack485' is not a Number. |
| 69 | +The String '-9859' is a Number. |
| 70 | +The String '784Will' is not a Number. |
| 71 | +The String '056' is a Number. |
| 72 | +The String 'Google' is not a Number. |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +## Java Program To Check If the Given String is Number or Not Using the `Character.isDigit()` method |
| 78 | + |
| 79 | +In this method we are iterating over an array of characters of the String and checking if any character is a digit or not. |
| 80 | + |
| 81 | +If we found one or more digit in the string, the given string is not a number string or a number. |
| 82 | + |
| 83 | +```Java |
| 84 | +import java.util.List; |
| 85 | + |
| 86 | +/** |
| 87 | + * A Java program to check if the given String is number of not. |
| 88 | + * using the Character.isDigit() method. |
| 89 | + * @author coderolls.com |
| 90 | + */ |
| 91 | +public class CheckIfNumber2 { |
| 92 | + |
| 93 | + public static void main(String[] args) { |
| 94 | + List<String> strings = List.of("95965", "Jack485", "-9859", "784Will", "056", "Google"); |
| 95 | + for(String str : strings) { |
| 96 | + boolean isNumber = isNumberOrNot(str); |
| 97 | + if (isNumber) { |
| 98 | + System.out.println("The String '" + str + "' is a Number."); |
| 99 | + } else { |
| 100 | + System.out.println("The String '" + str + "' is not a Number."); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * A method check if the input param string is a numeric or not |
| 108 | + * it is uses a regex Expression |
| 109 | + * -? --> negative sign, could have none or one |
| 110 | + * \\d+ --> one or more digits |
| 111 | + * @param str |
| 112 | + * @return |
| 113 | + */ |
| 114 | + private static boolean isNumberOrNot(String str) { |
| 115 | + char[] chars = str.toCharArray(); |
| 116 | + for(int i=0; i<str.length(); i++) { |
| 117 | + |
| 118 | + char ch = str.charAt(i); |
| 119 | + if(i==0 && ch == '-') { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + if (! (Character.isDigit(ch))) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + //If not a single ch is digit, the string is numeric |
| 128 | + return true; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +Output |
| 134 | + |
| 135 | +``` |
| 136 | +The String '95965' is a Number. |
| 137 | +The String 'Jack485' is not a Number. |
| 138 | +The String '-9859' is a Number. |
| 139 | +The String '784Will' is not a Number. |
| 140 | +The String '056' is a Number. |
| 141 | +The String 'Google' is not a Number. |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +## Java Program To Check If the Given String is Number or Not `NumberFormatExpression` way. |
| 147 | + |
| 148 | +We can also check if it using the `NumberFormatExpression` way. |
| 149 | + |
| 150 | +Let's See how. (**Not recommended to use, Just for Info**) |
| 151 | + |
| 152 | +We can try to parse the given String using the `Integer.parseInt(str)` method. |
| 153 | + |
| 154 | +If the given string is not a number the method will throw `NumberFormatExpression` and we can return `false` by catching this expression. Otherwise `true`. |
| 155 | + |
| 156 | +See the sample method below. |
| 157 | + |
| 158 | +```java |
| 159 | +public static boolean isInteger(String s) { |
| 160 | + try { |
| 161 | + Integer.parseInt(s); |
| 162 | + } catch(NumberFormatException e) { |
| 163 | + return false; |
| 164 | + } catch(NullPointerException e) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + // only got here if we didn't return false |
| 168 | + return true; |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +**But this way is not recommended as it causes expensive exception in the code.** |
0 commit comments