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.
Create a Java file
Create a Java file named FloatIsNaN.java in the ~/project directory:
cd ~/project
touch FloatIsNaN.java
Define the main method
Add the following code to the main method:
public class FloatIsNaN {
public static void main(String[] args) {
}
}
Declare and define float variables
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;
Test the Float.isNaN() method
Test 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
Compile and run the program
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
Test the Float.isNaN() method with user input
Add 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");
}
Compile and run the program
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
Test the Float.isNaN() method live
You 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");
}
}
}
Compile and run the program
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
Summary
In this lab, you have learned the following:
- Java
isNaN(float v)method is used to check whether a given float value is a NaN value or not. - The
isNaN()method returns the boolean valuetruefor NaN values andfalsefor non-NaN values. - You can test the
isNaN()method by declaring and defining float variables or by taking user input. - You can also test the
isNaN()method live with the example provided in the lab.
Good job!



