Java input methods Scanner and BufferedReader

Source: Internet
Author: User

Scanner in the Java.util package, you need to import java.util.*, or import java.util.Scanner;

How it works: by creating a Scanner object with new, scanner needs to pass in a system.in as a parameter, which we can consider to be the scanner of the data through its internal mechanism to wrap system.in. The scanner object passes through a series of in.nextxxx () methods to read the corresponding basic type of data through the In.hasnextxxx () method to determine if there is a next data. However, the scanner read data is press space, which includes the SPACEBAR, TAB key, and enter key. As soon as one of them is encountered, the scanner method returns the next input, which is another problem we will face, when we have a space in the input data, we will not get the data we want, so we will have to consider the Bufferreader to read the data!

Actual program:

1 Import java.util.scanner;2 public class Scannertester{3 public static void Main (string[] args) {4 System.out.print ("Plea SE input the name: "); 5 Scanner Scanner = new Scanner (system.in); 6 String name = Scanner.nextline (); 7 System.out.println (NA me); 8}              9}

  

Bufferreader is located in the Java.io package, you need to import java.io.*, or import java.io.BufferedReader;.

The use of Bufferreader is relatively not so many ways to let you choose, reading data is fixed, so the format is relatively single, just remember the method of reading the data. Bufferreader br = new Bufferreader (new InputStreamReader (system.in));. The Bufferreader object reads the data through the ReadLine () method, ReadLine () reads a row of data by pressing ENTER to enter, as long as it is readLine () before the Enter key, and the method returns, however, To use Bufferreader to enter data of a type other than some characters, it is relatively troublesome, need to pass some xxxx.parsexxx (), to convert the corresponding data type, although, some trouble, but by comparing on some OJ systems and scanner, Bufferreader efficiency is one times higher than scanner, this gap can be imagined, read more data, the effect is more obvious.

Actual program:

Import Java.io.bufferedreader;public class Bufferedreadertester{public static void Main (string[] args) { System.out.print ("Please input the adress:"); BufferedReader bufferedReader1 = new BufferedReader (new InputStreamReader (system.in)); String adress = Bufferedreader1.readline (); System.out.println (adress);}}

  

 

Java input methods Scanner and BufferedReader

Related Article

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.