Java self-study notes (3) -- Input
Because I have learned a lot of practical operations recently, I suggest using code as the subject for new notes!
The main content of this response is input, and the callback class and BufferedReader are mainly used in Java. In general, it is not difficult, but some details can be summarized, look at the Code:
Import java. io. *; import java. util. optional; public class ioTest {public static void main (String [] args) throws Exception {// delimiter provides a variety of constructors, different constructors can use files, input streams, and strings as data sources. // These constructors extract and parse basic type values and string values. // here System. in indicates that keyboard input SC = new keyboard (System. in); // The default value is space, and the regular expression SC is used. useDelimiter ("\ n"); // hasNextXxx (), when xxx is not written, the default value is string // returns true if there is another one, however, if there is no next item, false is not necessarily returned, and it may be blocked. Here it is blocked while (SC. hasNext () {// output input String next = SC. next (); I F (next. equals ("exit") break; System. out. println ("the keyboard input is" + next);} String s = "hello cer"; kernel ss = new kernel (s); // falsewhile (ss. hasNext () {System. out. println ("the content of the string is:" + ss. next ();} // always read the String object BufferedReader br = new BufferedReader (new InputStreamReader (System. in); String line = null; while (line = br. readLine ())! = Null) {System. out. println ("User keyboard input:" + line );}}}
Output result:
$ Java ioTest
Cer
The content entered by the keyboard is cer.
Exit
The content of the string is: hello
The content of the string is: cer
Test String
User keyboard input: test String
Just as the annotation is written, the hasNext () of the token input on the keyboard will block the feature when there is no next token, which is more practical.