Home > Java > javaTutorial > Use java's Scanner.next() function to read user-entered strings from the console

Use java's Scanner.next() function to read user-entered strings from the console

王林
Release: 2023-07-24 11:09:06
Original
1912 people have browsed it

Use java's Scanner.next() function to read user input strings from the console

In Java programming, it is often necessary to read user input from the console. The Scanner class is one of the commonly used tools in Java to obtain user input from the console. The Scanner class provides several methods for reading user input, one of the most commonly used is the next() function.

next() function is used to read the string entered by the user from the console. The read range is the characters from the current position to the next space or newline character, and then the read characters String returned.

The following is a sample code that demonstrates how to use the next() function to obtain the string entered by the user from the console:

import java.util.Scanner;

public class UserInputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("请输入您的姓名:");
        String name = scanner.next();
        
        System.out.println("请输入您的年龄:");
        int age = scanner.nextInt();
        
        System.out.println("您的姓名是:" + name);
        System.out.println("您的年龄是:" + age);
    }
}
Copy after login

In the above sample code, we first create a Scanner object scanner, which will read user input from standard input (System.in).

Then, we get the string entered by the user from the console by calling the scanner.next() function and store it in a string variable named name.

Next, we get the integer entered by the user from the console by calling the scanner.nextInt() function and store it in an integer variable named age.

Finally, we print the obtained name and age to the console.

It should be noted that the next() function can only read the string before the next space or newline character. If you need to read the entire line of input, you can use the nextLine() function.

In addition, it is necessary to note that the data type entered by the user matches the expected data type, otherwise an exception may be thrown. In the sample code above, we used the nextInt() function to read the age, so the user input should be an integer.

To summarize, using the next() function of the Scanner class can easily read the string entered by the user from the console. In practical applications, we can use different Scanner functions to read different types of data as needed. I hope this article helps you understand how to use the next() function to read user-entered strings from the console.

The above is the detailed content of Use java's Scanner.next() function to read user-entered strings from the console. 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template