Check if a given string is a valid number (Integer or Floating Point) in Java
Last Updated :
03 May, 2023
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.
Implementation:
Java
// 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");
}
}
}
Outputabc is not a valid integer number
1234 is a valid integer number
The time complexity of this program is O(1), because it performs a fixed set of operations that do not depend on the input size.
The space complexity of the program is also O(1), because it only uses a fixed amount of memory to store the two String variables input1 and input2.
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.
Implementation:
Java
// 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");
}
}
}
Output10e5.4 is not a valid float number
2e10 is a valid float number
The time complexity of this program is O(1) because it performs a constant number of operations to check each input string.
The space complexity is also O(1) because it uses a constant amount of memory to store the input strings and the exception objects.
For big numbers
We can use BigInteger and BigDecimal class constructors for large numbers.Below is the java program to demonstrate the same.
Implementation:
Java
// Java program to check whether given string
// is a valid number.
import java.math.BigDecimal;
import java.math.BigInteger;
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");
}
}
}
Output1231456416541214651356151564651954156 is a valid integer number
105612656501606510651e655.4 is not a valid float number
2e102225 is a valid float number
Similar Reads
Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach) In Set 1, we have discussed general approach to check whether a string is a valid number or not. In this post, we will discuss regular expression approach to check for a number. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input :
3 min read
Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach) Validate if a given string is numeric. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input : 10e5.4 Output : false The following cases need to be handled in the code. Ignore the leading and trailing white spaces.Ignore the '+', '-'
11 min read
Check whether given floating point number is even or odd Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number
6 min read
Program to check if input is an integer or a string Write a function to check whether a given input is an integer or a string. Definition of an integer : Every element should be a valid digit, i.e '0-9'. Definition of a string : Any one element should be an invalid digit, i.e any symbol other than '0-9'. Examples: Input : 127Output : IntegerExplanati
15+ min read
Check whether the given floating point number is a palindrome Given a floating-point number N, the task is to check whether it is palindrome or not. Input: N = 123.321 Output: YesInput: N = 122.1 Output: No Approach: First, convert the given floating-point number into a character array.Initialize the low to first index and high to the last index.While low <
4 min read
Java Program to Check if all digits of a number divide it Given a number n, find whether all digits of n divide it or not.Examples: Input : 128 Output : Yes 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130 Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128
2 min read
Check if a number is in given base or not Given a number as a string and a base, check if the given number is in the given base or not. Examples: Input : str = "1010", base = 2 Output : Yes Input : str = "1015", base = 2 Output : No Input : str = "AF87", base = 16 Output : Yes The idea is to one by one check if all digits are in the given b
10 min read
Check whether a given number N is a Nude Number or not Given an integer N, the task is to check whether N is a Nude number or not. A Nude number is a number that is divisible by all of its digits (which should be nonzero). Example: Input: N = 672 Output: Yes Explanation: Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output
4 min read
Check if a Float value is equivalent to an Integer value Given a floating-point number N, the task is to check if the value of N is equivalent to an integer or not. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: N = 1.5Output: NO Input: N = 1.0Output: YES Approach: The idea is to use the concept of Type Casting. Follow the
3 min read
Program to check if a number is Positive, Negative, Odd, Even, Zero Given a number n, the task is to check whether the given number is positive, negative, odd, even, or zero. Method 1 : A number is positive if it is greater than zero. We check this in the expression of if.If it is False, the number will either be zero or negative.This is also tested in subsequent ex
15 min read