Java Scanner nextFloat() Method

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The nextFloat() method in Java, part of the java.util.Scanner class, is used to retrieve the next token from the input as a float value. This method is useful for reading and processing floating-point values from the input.

Table of Contents

  1. Introduction
  2. nextFloat() Method Syntax
  3. Understanding nextFloat()
  4. Examples
    • Basic Usage
    • Handling Input Errors
  5. Real-World Use Case
  6. Conclusion

Introduction

The nextFloat() method returns the next token from the scanner's input as a float. This method is useful when you need to read and process floating-point values, which represent decimal numbers.

nextFloat() Method Syntax

The syntax for the nextFloat() method is as follows:

public float nextFloat() 

Parameters:

  • This method does not take any parameters.

Returns:

  • The next token as a float value.

Throws:

  • InputMismatchException: If the next token does not match the float regular expression, or is out of range.
  • NoSuchElementException: If no more tokens are available.
  • IllegalStateException: If the scanner is closed.

Understanding nextFloat()

The nextFloat() method retrieves the next token and converts it to a float. If the token cannot be interpreted as a float, an InputMismatchException is thrown.

Examples

Basic Usage

To demonstrate the basic usage of nextFloat(), we will create a Scanner object and use it to read float values from a string.

Example

import java.util.Scanner; public class NextFloatExample { public static void main(String[] args) { String input = "10.5 20.5 30.5"; // Create Scanner object in try-with-resources to ensure it closes automatically try (Scanner scanner = new Scanner(input)) { while (scanner.hasNextFloat()) { float value = scanner.nextFloat(); System.out.println("Float value: " + value); } } // Scanner is automatically closed here } } 

Output:

Float value: 10.5 Float value: 20.5 Float value: 30.5 

Handling Input Errors

This example shows how to handle errors when the input token cannot be interpreted as a float.

Example

import java.util.InputMismatchException; import java.util.Scanner; public class HandleInputErrorsExample { public static void main(String[] args) { String input = "10.5 abc 20.5"; // Create Scanner object in try-with-resources to ensure it closes automatically try (Scanner scanner = new Scanner(input)) { while (scanner.hasNext()) { try { float value = scanner.nextFloat(); System.out.println("Float value: " + value); } catch (InputMismatchException e) { System.out.println("Invalid input: " + scanner.next()); } } } // Scanner is automatically closed here } } 

Output:

Float value: 10.5 Invalid input: abc Float value: 20.5 

Real-World Use Case

Reading Configuration Data

In real-world applications, the nextFloat() method can be used to read and process floating-point configuration data from a file.

Example

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ConfigParser { public static void main(String[] args) { try (Scanner scanner = new Scanner(new File("config.txt"))) { while (scanner.hasNext()) { try { float value = scanner.nextFloat(); System.out.println("Configuration value: " + value); } catch (InputMismatchException e) { System.out.println("Invalid input: " + scanner.next()); } } } catch (FileNotFoundException e) { System.out.println("File not found: " + e.getMessage()); } // Scanner is automatically closed here } } 

Output (Assuming config.txt contains valid and invalid float values):

Configuration value: 10.5 Invalid input: abc Configuration value: 20.5 ... 

Conclusion

The Scanner.nextFloat() method is used to retrieve the next token from the input as a float value. This method is particularly useful for applications requiring floating-point input values. By understanding and using this method, you can efficiently parse and handle float input data. Always close the Scanner using try-with-resources to ensure proper resource management.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare