Home  >  Article  >  Java  >  How to get the input value in java

How to get the input value in java

下次还敢
下次还敢Original
2024-04-21 02:57:571077browse

The methods for obtaining input in Java are: use the Scanner class to read text input; use the BufferedReader class to read text input; use the Console class to read text input (only applicable to Java 9).

How to get the input value in java

How to get the entered value in Java

In Java you can use System.in to get the user input of.

1. Use Scanner class

<code class="java">import java.util.Scanner;

public class InputExample {

    public static void main(String[] args) {
        // 创建 Scanner 对象以从 System.in 读取输入
        Scanner scanner = new Scanner(System.in);

        // 从控制台读取一行输入
        String input = scanner.nextLine();

        // 打印输入
        System.out.println("您输入的是:" + input);
    }
}</code>

2. Use BufferedReader class

<code class="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputExample {

    public static void main(String[] args) throws IOException {
        // 创建 BufferedReader 对象以从 System.in 读取输入
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        // 从控制台读取一行输入
        String input = reader.readLine();

        // 打印输入
        System.out.println("您输入的是:" + input);
    }
}</code>

3. Use Console class (Java 9 only)

<code class="java">import java.util.console.Console;

public class InputExample {

    public static void main(String[] args) {
        // 创建 Console 对象以从 System.in 读取输入
        Console console = System.console();

        // 从控制台读取一行输入
        String input = console.readLine();

        // 打印输入
        System.out.println("您输入的是:" + input);
    }
}</code>

The above is the detailed content of How to get the input value in java. 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