Java Float isNaN Method

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/booleans("`Booleans`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117670{{"`Java Float isNaN Method`"}} java/classes_objects -.-> lab-117670{{"`Java Float isNaN Method`"}} java/class_methods -.-> lab-117670{{"`Java Float isNaN Method`"}} java/exceptions -.-> lab-117670{{"`Java Float isNaN Method`"}} java/modifiers -.-> lab-117670{{"`Java Float isNaN Method`"}} java/oop -.-> lab-117670{{"`Java Float isNaN Method`"}} java/packages_api -.-> lab-117670{{"`Java Float isNaN Method`"}} java/user_input -.-> lab-117670{{"`Java Float isNaN Method`"}} java/wrapper_classes -.-> lab-117670{{"`Java Float isNaN Method`"}} java/identifier -.-> lab-117670{{"`Java Float isNaN Method`"}} java/arrays -.-> lab-117670{{"`Java Float isNaN Method`"}} java/booleans -.-> lab-117670{{"`Java Float isNaN Method`"}} java/comments -.-> lab-117670{{"`Java Float isNaN Method`"}} java/data_types -.-> lab-117670{{"`Java Float isNaN Method`"}} java/if_else -.-> lab-117670{{"`Java Float isNaN Method`"}} java/operators -.-> lab-117670{{"`Java Float isNaN Method`"}} java/output -.-> lab-117670{{"`Java Float isNaN Method`"}} java/strings -.-> lab-117670{{"`Java Float isNaN Method`"}} java/variables -.-> lab-117670{{"`Java Float isNaN Method`"}} java/system_methods -.-> lab-117670{{"`Java Float isNaN Method`"}} end

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 value true for NaN values and false for 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!

Other Java Tutorials you may like