In this tutorial, you will learn how to check if a String is an Integer in Java. To achieve this, first, you will learn how to use the matches()
method of the String class. Second, you will use the Integer.parseInt()
method of the Integer class.
Finally, you will use the Integer.valueOf()
method of Integer
class.
Using the matches() method
To check if a String is a number, you can use regular expressions to find Strings with a number. Let’s look at an example.
package com.javawhizz; import java.util.Arrays; public class Main { public static void main(String[] args) { String[] names = {"John", "doe", "35"}; boolean containsNumber = Arrays.stream(names) .anyMatch(name -> name.matches("-?\\d+")); if (containsNumber) { System.out.println("The array contains a string with a number"); } else { System.out.println("No number found"); } } }
The array of strings in this example contains the data to check whether a number is present.
Since you are leveraging arrays, there is no need of creating a custom method. You should leverage the anyMatch()
method of the Java Stream.
This method returns true if the boolean argument specified evaluates to true. Otherwise, the method returns false.
As a result, pass the matches()
method as the argument and specify the regular expression to match a number. Since the array has a String with a number, the anyMatch()
method will return true.
Output
The array contains a string with a number
Using Integer.parseInt() method
The Integer.parseInt()
method only passes strings that contain numbers. Additionally, the numbers should be decimal digits except for the first character.
The first character can be a minus sign for a negative number or plus sign for a positive number. Let’s look at an example.
package com.javawhizz; import java.util.Arrays; public class Main { public static void main(String[] args) { String[] names = {"John", "doe", "35"}; boolean containsNumber = Arrays.stream(names) .anyMatch(string -> { try { Integer.parseInt(string); return true; } catch (NumberFormatException e) { return false; } }); if (containsNumber){ System.out.println("The array contains a string which is a number"); }else { System.out.println("No number found"); } } }
If a String does not have a decimal digit, the method throws an exception. The exception thrown in this case is the NumberFormatException
.
As a result, you should add a try-catch block in the anyMatch()
method to catch the exception. Additionally, ensure the method returns true if a parsable integer is found.
If a parsable integer is not found, the method will throw an exception. To catch the exception, return false. Note that the method will return true for this example.
Output
The array contains a string with a number
Using Integer.valueOf() method
The Integer.valueOf()
method returns an Integer object of the integer value held by the String. Let’s look at an example.
package com.javawhizz; import java.util.Arrays; public class Main { public static void main(String[] args) { String[] names = {"John", "doe", "35"}; boolean containsANumber = Arrays.stream(names) .anyMatch(string -> { try { Integer.valueOf(string); return true; } catch (NumberFormatException e) { return false; } }); if (containsANumber){ System.out.println("The array contains a String which is a number"); }else { System.out.println("No number found"); } } }
As you can see from the example, this method works the same way as the previous one. The only difference is that it returns an Integer object.
Note that this will work if the String contains a parsable Integer. Otherwise, the method will throw a NumberFormatException
. Use the try-catch block to handle the exception by returning false.
Output
The array contains a string which is a number
Conclusion
In this tutorial, you have learned how to check if a String is an Integer in Java.
First, you have learned how to use the matches()
method of the String class. In the second example, you have learned how to use the Integer.parseInt()
method. In the last example, you have learned how to use the Integer.valueOf()
method.
Happy Hacking!
0 Comments