## Java Program to Check if a Number is Float or Integer Here, I’m going to present the tutorial with the concept of the Java Program to Check if a Number is Float or Integer. Initially, We will discuss an integer and a floating number and how to do a task using the java program?

Contents

## What is Int and Float?

An integer is a number without decimal points whereas a number with decimal points is called a floating-point number.

Examples for Integer : -321, 497, 19345, and -976812.

Examples for Float : 5.5, 0.001, and -2,345.6789. Let’s start the article.

## Java Program to Check if a Number is Float or Integer

This is an example program to check if a number is an integer or not.

``````//Java program to check whether given string
// is a valid integer number

class CD
{
public static void main (String[] args)
{
String input1 = "Codingdiksha";
String input2 = "2021";

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");
}
}
}
``````

## Execution

Initially, We have to declare the two inputs with the respective variables. After executing this program, The parseInt() method will check if a number is an integer or not.

if it is true then it will print “It is a valid integer number”. Otherwise, It will print “It is not a valid integer number” using the system.out.printIn() function.

## Output

Below, You can see the output of the Java Program to Check if a Number is an integer or not.

``````Codingdiksha is not a valid integer number
2021 is a valid integer number``````
```Read Also
JavaScript Program to Check if a Number is Float or Integer
Check if a Number is Positive or Negative using C++
Check if a Number is Positive or Negative using Python```

## Java Program to Check if a Number is Float or Integer

Here, We can check if a number is a floating-point number or not using the below program.

``````// Java program to check whether given string
// is a valid float number.

class CD
{
public static void main (String[] args)
{
String input1 = "y5.8";
String input2 = "9u64";

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");
}
}
}
``````

## Explanation

Both program executions are the same but the only difference is both inputs and outputs.

## Output

This is the output of check if a number is a floating-point number or not.

``````y5.8 is not a valid float number
9u64 is a valid float number``````

## Java Program to Check if a Number is Float or Integer

We can execute a task of a java program to check if a number is a float or integer using the below source code.

``````// Java program to check whether given string
// is a valid number.

import java.math.BigInteger;
import java.math.BigDecimal;

class CD
{
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");
}

}
}
``````

## Execution

Initially, We need to import the java.math.BigInteger() and java.math.BigDecimal() classes into this program. After declaring the three inputs with variables, We will compile the program.

After compiling the program, We will check if a number is an integer or float using the BigInteger constructor and BigDecimal constructor respectively.

If the inputs are valid then the output is like “It is a valid either integer or floating-point number”. Otherwise, “It is not a valid either integer or floating-point number using the system.out.printIn() function.

## Output

The output of the Java Program to Check if a Number is Float or Integer will be provided below for your reference.

``````1231456416541214651356151564651954156 is a valid integer number
105612656501606510651e655.4 is not a valid float number
2e102225 is a valid float number``````

## Final Words

We hope this article helps you to learn the Java program to check if a number is float or integer. Do share this study with other Java learners via social media. If you have any doubts regarding this article? then let me know via the comment section.

### Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.