First, import the java.util.* package. (Recommended learning: java course)
import java.util.*;
Then, you need to create a new scanner object that reads standard input (keyboard).
Scanner in = new Scanner(System.in);
Now, you can enter strings from the keyboard.
String s = in.nextLine();
The above line reads a line of string input from the keyboard into the variable s.
Please see a complete simple example:
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); System.out.println(s); } }
The above is the detailed content of How to enter a string in java. For more information, please follow other related articles on the PHP Chinese website!