Home >Java >javaTutorial >Java input stream Scanner/BufferedReader usage example
1. Use Scanner
When using it, you need to introduce the package import java.util.Scanner; first define the Scanner object
Scanner sc = new Scanner(System.in);
If To input an integer, then int n = sc.nextInt();
String type, then String temp = sc.next();
For example:
import java.util.Scanner; public class Test { @SuppressWarnings("resource") public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(scanner.next()); } }
2. Use BufferedReader
You need to introduce import java.io.Reader before use;
BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();
For example:
import java.io.*; public class Test { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); System.out.println(br.readLine()); } catch (IOException e) { } } }
For more Java input stream Scanner/BufferedReader usage examples and related articles, please pay attention to the PHP Chinese website!