Java programming: three ways to get input

1. Get single character input—System.in

Note: System.in can only receive a single character, and output the ASCLL value of the character. If you need to output characters, you need to force type conversion.

Receive a number

public static void scaneNum(){
    
    
    int scane = 0;
    System.out.print("请输入数据:");
    try {
    
    
        scane = System.in.read();
    } catch (Exception e) {
    
    
        e.printStackTrace();
    }
    System.out.println("输入的数据为:" + scane);
}

Run result: only receive the first number
Insert picture description here

Receive a character

public static void scaneChar(){
    
    
    char scane = '0';
    System.out.print("请输入数据:");
    try {
    
    
        scane = (char)System.in.read();
    } catch (Exception e) {
    
    
        e.printStackTrace();
    }
    System.out.println("输入的数据为:" + scane);
}

Run result: only the first character is received
Insert picture description here

package testScane;

public class test {
    
    
    public static void main(String[] args) {
    
    
        //scaneNum();
        scaneChar();
    }

    public static void scaneNum(){
    
    
        int scane = 0;
        System.out.print("请输入数据:");
        try {
    
    
            scane = System.in.read();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        System.out.println("输入的数据为:" + scane);
    }

    public static void scaneChar(){
    
    
        char scane = '0';
        System.out.print("请输入数据:");
        try {
    
    
            scane = (char)System.in.read();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        System.out.println("输入的数据为:" + scane);
    }
}

2. Get a line of string input: Scanner

Note: When using Scanner, you need to import the Scanner package in util.
① The next() method is used to obtain the character string input by the user.
② NextInt() converts the obtained input string to an integer type.
③ NextFloat() converts to a floating point type (two decimal places are reserved by default).
④ NextBoolean() is converted to a boolean type

package testScane;

import java.util.Scanner;

public class test1 {
    
    
    public static void main(String[] args) {
    
    
        scane();
        scaneInt();
        scaneFloat();
        scaneBool();
    }

    public static void scane() {
    
    
        System.out.print("请输入数据:");
        Scanner scan = new Scanner(System.in);
        String read = scan.nextLine();
        System.out.println("输入的数据为:" + read);
    }

    public static void scaneInt() {
    
    
        System.out.print("请输入整形数据:");
        Scanner scan = new Scanner(System.in);
        int scanInt = scan.nextInt();
        System.out.println("输入的数据为:" + scanInt);
    }

    public static void scaneFloat() {
    
    
        System.out.print("请输入浮点型数据:");
        Scanner scan = new Scanner(System.in);
        float scanFloat = scan.nextFloat();
        System.out.println("输入的数据为:" + scanFloat);
    }

    public static void scaneBool() {
    
    
        System.out.print("请输入数据:");
        Scanner scan = new Scanner(System.in);
        boolean scanBoolean = scan.nextBoolean();
        System.out.println("输入的数据为:" + scanBoolean);
    }
}

Operation result:
Insert picture description here
Note: the difference between next() and nextLine()

  • The next() method does not accept spaces. Before receiving valid data, all spaces, tab keys and other input will be ignored. After receiving the first valid data, if you press space or tab, any subsequent input will be invalid until you press Enter to exit.
  • nextLine() can accept a space or tab key, and its input ends with the enter key.
package testScane;

import java.util.Scanner;

public class test4 {
    
    
    public static void main(String[] args) {
    
    
        scan1();
        scan2();
    }

    public static void scan1(){
    
    
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        System.out.println(str);
    }

    public static void scan2(){
    
    
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        System.out.println(str);
    }
}

operation result:Insert picture description here

3. Get a line of string input: BufferedReader

package testScane;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test3 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        scane();
    }

    public static void scane() throws IOException {
    
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String scane = null;
        System.out.print("输入数据:");
        scane = br.readLine();
        System.out.println("您输入的数据为:" + scane);
    }
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108598152