An overview of the

When you need to input literal data into a program, you can use the standard InputStream stream System.in to get the data of a bit and the asciI-encoded integer of a byte through its provided read() method.

Scanner

Get the user input string:

import java.util.Scanner;

public class HelloCc {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please input your name: ");
        System.out.printf("Hello! %s!", scanner.next()); }}Copy the code

Execution Result:

Please input your name:

Hello! Cc!

Similar methods: nextInt(),nextFloat(),nextBoolean,nextLine()

Note: Scanner’s next() method takes a blank byte, and Scanner passes the next input, whether it’s a space, TAB, or Enter key

BufferedReader

Although Scanner is convenient to use, it separates each character with a blank string and is not useful when the user enters a full string containing blank bytes. At this point, you can use the java.io package to build BufferedReader to create a buffer for the standard input stream.

Get user input (including blank bytes):

import java.io.*;

public class HelloCc {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Please input text: "); String text = buf.readLine(); System.out.println(text); }}Copy the code

Execution Result:

Please input text:

I am Cc!

Note: The readLine() method must handle IOException