In the article Check if a given string is a valid number, we have discussed general approach to check whether a string is a valid number or not. In Java we can use Wrapper classes parse() methods along with try-catch blocks to check for a number.
For integer number
Integer class provides a static method parseInt() which will throw NumberFormatException if the String does not contain a parsable int. We will catch this exception using catch block and thus confirm that given string is not a valid integer number.Below is the java program to demonstrate the same.
//Java program to check whether given string // is a valid integer number class GFG { public static void main (String[] args) { String input1 = "abc"; String input2 = "1234"; try { // checking valid integer using parseInt() method Integer.parseInt(input1); System.out.println(input1 + " is a valid integer number"); } catch (NumberFormatException e) { System.out.println(input1 + " is not a valid integer number"); } try { // checking valid integer using parseInt() method Integer.parseInt(input2); System.out.println(input2 + " is a valid integer number"); } catch (NumberFormatException e) { System.out.println(input2 + " is not a valid integer number"); } } } |
Output:
abc is not a valid integer number 1234 is a valid integer number
For float number
Float class provides a static method parseFloat() which will throw NumberFormatException if the String does not contain a parsable float. We will catch this exception using catch block and thus confirm that given string is not a valid float number.If string is null, this method will throw NullPointerException.Below is the java program to demonstrate the same.
// Java program to check whether given string // is a valid float number. class GFG { public static void main (String[] args) { String input1 = "10e5.4"; String input2 = "2e10"; try { // checking valid float using parseInt() method Float.parseFloat(input1); System.out.println(input1 + " is a valid float number"); } catch (NumberFormatException e) { System.out.println(input1 + " is not a valid float number"); } try { // checking valid float using parseInt() method Float.parseFloat(input2); System.out.println(input2 + " is a valid float number"); } catch (NumberFormatException e) { System.out.println(input2 + " is not a valid float number"); } } } |
Output:
10e5.4 is not a valid float number 2e10 is a valid float number
For big numbers
We can use BigInteger and BigDecimal class constructors for large numbers.Below is the java program to demonstrate the same.
// Java program to check whether given string // is a valid number. import java.math.BigInteger; import java.math.BigDecimal; class GFG { public static void main (String[] args) { String input1 = "1231456416541214651356151564651954156"; String input2 = "105612656501606510651e655.4"; String input3 = "2e102225"; try { // checking valid integer number using BigInteger constructor new BigInteger(input1); System.out.println(input1 + " is a valid integer number"); } catch (NumberFormatException e) { System.out.println(input1 + " is not a valid integer number"); } try { // checking valid float number using BigDecimal constructor new BigDecimal(input2); System.out.println(input2 + " is a valid float number"); } catch (NumberFormatException e) { System.out.println(input2 + " is not a valid float number"); } try { // checking valid float number using BigDecimal constructor new BigDecimal(input3); System.out.println(input3 + " is a valid float number"); } catch (NumberFormatException e) { System.out.println(input3 + " is not a valid float number"); } } } |
Output:
1231456416541214651356151564651954156 is a valid integer number 105612656501606510651e655.4 is not a valid float number 2e102225 is a valid float number
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Check whether the given floating point number is a palindrome
- Check whether given floating point number is even or odd
- Floating Point Operations & Associativity in C, C++ and Java
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Problem in comparing Floating point numbers and how to compare them correctly?
- Check whether the given string is a valid identifier
- Java Program to Illustrate the Usage of Floating
- Check if email address valid or not in Java
- Check if URL is valid or not in Java
- Program to Convert Set of Integer to Array of Integer in Java
- Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
- Check if given email address is valid or not in C++
- Program to check valid mobile number
- Program to check for a Valid IMEI Number
- How to check Aadhar number is valid or not using Regular Expression
- Program to generate all possible valid IP addresses from given string
- Program to generate all possible valid IP addresses from given string | Set 2
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition

