Java Fundamentals Hardening IO Stream Note 62: Three ways to implement keyboard entry

Source: Internet
Author: User

1. Three ways to achieve keyboard input

System.in the standard input stream. is to get the data from the keyboard

keyboard input data three ways :
a: The args receive parameter of the Main method.
Java HelloWorld Hello World Java
B : Scanner (JDK5 later)
Scanner sc = new Scanner (system.in);
String s = sc.nextline ();
int x = Sc.nextint ()
C : standard input stream implementation via character buffer stream packaging
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));

2. Code implementation:

1  Packagecn.itcast_04;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 7 /*8 * system.in standard input stream. is to get the data from the keyboard9  * Ten * Keyboard input data: One * A:The args receive parameter of the Main method.  A * java HelloWorld Hello world Java - * B:Scanner (after JDK5)  - * Scanner sc = new Scanner (system.in); the * String s = sc.nextline (); - * int x = Sc.nextint () - * C: implementation of standard input stream via character Buffer stream packaging  - * BufferedReader br = new BufferedReader (new InputStreamReader (system.in)); +  */ -  Public classSystemindemo { +      Public Static voidMain (string[] args)throwsIOException { A         // //get standard input stream at         //InputStream is = system.in; -         // //Do I have to get one row at a time? -         // //line.  -         // //How to achieve it? -         // //To achieve this, first you need to know which way to read one row of data at a time? -         // //ReadLine () in         // //and which class is this method in? -         // //BufferedReader to         // //So, you should create the BufferedReader object this time, but the underlying is still using the standard input stream +         // //bufferedreader br = new BufferedReader (is); -         // //according to our supposition, it should be possible now, but it's an error. the         // //The reason for this is that the character buffer stream can only be manipulated for character streams, and you are now a byte stream, so it can't be used? *         // //So, I still want to use, please give me a solution? $         // //converts a byte stream to a character stream, and then flows through a character bufferPanax Notoginseng         //InputStreamReader ISR = new InputStreamReader (is); -         //BufferedReader br= New BufferedReader (ISR); theBufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); +  ASystem.out.println ("Please enter a string:"); theString line =br.readline (); +System.out.println ("The string you entered is:" +Line ); -  $System.out.println ("Please enter an integer:"); $         //int i = Integer.parseint (Br.readline ()); -line =br.readline (); -         inti =Integer.parseint (line); theSystem.out.println ("The Integer you entered is:" +i); -     }Wuyi}

Run the effect as follows:

Java Fundamentals Hardening IO Stream Note 62: Three ways to implement keyboard entry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.