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.
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.
10e5.4 is not a valid float number 2e10 is a valid float number
For big numbers
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 email@example.com. 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.
- 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 given floating point number is even or odd
- Check whether the given floating point number is a palindrome
- 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
- 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 email address valid or not in Java
- Check if URL is valid or not in Java
- Check if given email address is valid or not in C++
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- 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
- Floating Action Button (FAB) in Android with Example