Home  >  Article  >  Java  >  How to read floating point number from user input using nextFloat() method of Scanner class

How to read floating point number from user input using nextFloat() method of Scanner class

WBOY
WBOYOriginal
2023-07-25 14:42:191247browse

How to use the nextFloat() method of the Scanner class to read floating point numbers from user input

The Scanner class is one of the commonly used tools in Java to read input from the console or file. It provides many methods to read various different types of data, including the method nextFloat() which reads floating point numbers from user input. This article will introduce how to use the nextFloat() method of the Scanner class to obtain the floating point number entered by the user.

First, we need to introduce the Scanner class into the Java code. We can achieve this using the following statement:

import java.util.Scanner;

Next, we need to create a Scanner object to read user input. We can use the following code to create a Scanner object named scanner:

Scanner scanner = new Scanner(System.in);

With the System.in parameter, the Scanner object will read user input from the console .

We can read the floating point number entered by the user by calling the nextFloat() method of the Scanner object. For example, we can store a floating point input into a variable named inputFloat using the following statement:

float inputFloat = scanner.nextFloat();

In this example, the user will enter a floating point number and the value will be stored In the inputFloat variable. If the user input is not a floating point number, the program will throw an InputMismatchException. In order to handle this exception situation correctly, we can use the try-catch statement to catch the exception. The following is a complete sample code:

import java.util.Scanner;

public class ReadFloatExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        float inputFloat = 0;
        
        try {
            System.out.print("请输入一个浮点数:");
            inputFloat = scanner.nextFloat();
            
            System.out.println("你输入的浮点数是:" + inputFloat);
        } catch (Exception e) {
            System.out.println("输入不合法,请重新输入一个浮点数。");
        }
        
        scanner.close();
    }
}

In this example, we first prompt the user to enter a floating point number, and then store the user input into inputFloat by calling the scanner.nextFloat() method in variables. If the user input is not a floating point number, an exception will be thrown. We will catch this exception and prompt the user to re-enter a floating point number. Regardless of whether the user input is legal or not, we will finally call the scanner.close() method to close the Scanner object to prevent memory leaks.

The above is an introduction to the method of reading floating point numbers from user input using the nextFloat() method of the Scanner class. Hopefully this article helps you better understand how to correctly read user-entered floating point numbers.

The above is the detailed content of How to read floating point number from user input using nextFloat() method of Scanner class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn