Introduction
In this lab, you will learn about Java isNaN(float v)
method, which is used to check whether the float value passed is Not-a-Number(NaN) or not.
In this lab, you will learn about Java isNaN(float v)
method, which is used to check whether the float value passed is Not-a-Number(NaN) or not.
Create a Java file named FloatIsNaN.java
in the ~/project
directory:
cd ~/project
touch FloatIsNaN.java
main
methodAdd the following code to the main
method:
public class FloatIsNaN {
public static void main(String[] args) {
}
}
Declare and define float variables named f1
, f2
, f3
, f4
, and f5
:
float f1 = 67.78f;
float f2 = 0f;
float f3 = -f1/0.0f;
float f4 = f2/0.0f;
float f5 = 0.0f/0.0f;
Float.isNaN()
methodTest whether the float values are NaN values by using the Float.isNaN()
method:
System.out.println("The value is : " +Float.isNaN(f1)); //returns false for finite value
System.out.println("The value is : " +Float.isNaN(f2)); //returns false for infinite value
System.out.println("The value is : " +Float.isNaN(f3)); //returns false for infinite value
System.out.println("The value is : " +Float.isNaN(f4)); //returns true for NaN value
System.out.println("The value is : " +Float.isNaN(f5)); //returns true for NaN value
Save the changes to the file and compile the Java program using the javac
command:
javac FloatIsNaN.java
Then, run the program using the java
command:
java FloatIsNaN
Float.isNaN()
method with user inputAdd the following code after step 3 to test the Float.isNaN()
method with user input:
try {
System.out.println("Enter the value");
Scanner sc = new Scanner(System.in);
float i = sc.nextFloat();
boolean b = Float.isNaN(i);
if(b == true) {
System.out.println("Value is NaN");
} else {
System.out.println("Value is non NaN");
}
} catch(Exception e) {
System.out.println("Invalid Input");
}
Save the changes to the file and compile the Java program using the javac
command:
javac FloatIsNaN.java
Then, run the program using the java
command:
java FloatIsNaN
Float.isNaN()
method liveYou can also test the Float.isNaN()
method live with the following example:
import java.lang.Float;
import java.util.Scanner;
public class FloatIsNaN {
public static void main(String[] args) {
try {
System.out.println("Enter the value");
Scanner sc = new Scanner(System.in);
float i = sc.nextFloat();
boolean b = Float.isNaN(i);
if(b == true) {
System.out.println("Value is NaN");
} else {
System.out.println("Value is non NaN");
}
} catch(Exception e) {
System.out.println("Invalid Input");
}
}
}
Save the changes to the file and compile the Java program using the javac
command:
javac FloatIsNaN.java
Then, run the program using the java
command:
java FloatIsNaN
In this lab, you have learned the following:
isNaN(float v)
method is used to check whether a given float value is a NaN value or not.isNaN()
method returns the boolean value true
for NaN values and false
for non-NaN values.isNaN()
method by declaring and defining float variables or by taking user input.isNaN()
method live with the example provided in the lab.Good job!