Home > Java > Java Tutorial > body text

Java uses Scanner class to implement console input

王林
Release: 2023-07-24 13:57:19
Original
1673 people have browsed it

Java uses the Scanner class to implement console input

In Java, the Scanner class is a very powerful tool that can help us achieve the function of obtaining user input from the console. Through the Scanner class, we can easily read various types of data input by the user, including integers, floating point numbers, strings, etc. Let's take a look at how to use the Scanner class to implement console input.

First, we need to import the Scanner class in the code:

import java.util.Scanner;
Copy after login

Then, we can create the Scanner object and start using it to read user input. The following is a simple sample code:

public class ConsoleInputExample {
    public static void main(String[] args) {
        // 创建Scanner对象
        Scanner scanner = new Scanner(System.in);
        
        // 从控制台读取整数
        System.out.print("请输入一个整数:");
        int num = scanner.nextInt();
        System.out.println("您输入的整数是:" + num);
        
        // 从控制台读取浮点数
        System.out.print("请输入一个浮点数:");
        float fnum = scanner.nextFloat();
        System.out.println("您输入的浮点数是:" + fnum);
        
        // 从控制台读取字符串
        System.out.print("请输入一个字符串:");
        String str = scanner.nextLine();
        System.out.println("您输入的字符串是:" + str);
        
        // 关闭Scanner对象
        scanner.close();
    }
}
Copy after login

In the above code, we first create a Scanner object and pass it into the System.in parameter, indicating that we will read the user from the standard input stream input of. We can then use the various methods of the Scanner object to read different types of input.

In the sample code, we first use the nextInt() method to read the integer and save it in the num variable. Then, use the nextFloat() method to read the floating point number and save it in the fnum variable. Finally, use the nextLine() method to read a line of string and save it in the str variable.

It should be noted that if we read a string immediately after reading an integer or floating point number, we will find that the read string is empty. This is because the nextInt() or nextFloat() method only reads the numerical value and does not read the carriage return character. In order to solve this problem, we can call the scanner.nextLine() method to consume the carriage return character after reading the value.

In addition, it should be noted that after we have finished using the Scanner object, we should close the Scanner object by calling the scanner.close() method to release resources.

In summary, by using the Scanner class, we can implement the function of obtaining user input from the console in Java. Whether it is an integer, floating point number or string, it can be easily read using the Scanner class. Hope this article can help everyone.

The above is the detailed content of Java uses Scanner class to implement console input. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!