1. Using the Java.util.Scanner class
Using the Scannerd Next () method, the test code reads as follows:
- Scanner sc=New Scanner (system.in);
- System.out.println ("Waiting Inputchar:");
- String N=sc.next ();
- SYSTEM.OUT.PRINTLN (n);
Of course remember to introduce the relevant package:
- Import Java.util.Scanner;
2. Using the BufferedReader class to obtain user input
BufferedReader class is a subclass of Java.IO.Reader
The instance code is as follows:
- Using BufferedReader to get user input
- BufferedReader receives an instance of the Java.io.reader class
- Here we can use Java. Io. InputStreamReader
- When using flow operation, remember to add exception handling, or you will report an error
- Try
- {
- BufferedReader bt=New BufferedReader (new InputStreamReader (system.in));
- String S=bt.readline ();
- System.out.println (s);
- }
- catch (Java.io.IOException ex)
- {
- System.out.println (Ex.getmessage ());
- }
Here to add a try. catch () exception handling block, or the following exception is reported:
- Exception in thread "main" java.lang.Error:Unresolved compilation problem:
- Unhandled Exception type IOException
Ways to get user input in Java