Comparison of BufferedReader and scanner in Java

Source: Internet
Author: User

The Scanner and BufferedReader also enable the input of the keyboard data into the program,

Import java.io.*;
Import Java.util.Scanner;
public class C {
public static void Main (String []args) throws IOException
{

String x1,x2;
int sum=0;

System.out.print ("BufferedReader method \ninput Number:");

    // The BufferedReader object only considers the carriage return as the end of the input, the resulting string
    bufferedreader myreader=new BufferedReader ( New InputStreamReader (system.in));

    x1=myreader.readline ();
    x2=myreader.readline ();
    int a=integer.parseint (x1);
    int B=integer.parseint (x2);
    sum=a+b;
    system.out.printf ("sum=%d", Sum);

    system.out.println ("\n\nscanner method");
    scanner sc=new Scanner (system.in);
    int a1,b1;
    a1=sc.nextint ();   

//scanner object to return, space, tab is the end of the input, directly with Sc.next () to get a string form
B1=sc.nextint ();
System.out.print ("sum=" + (A1+B1));
}
}

BufferedReader is a character input stream that reads text, buffers individual characters, and provides efficient reading of characters, arrays, and rows! Faster than scanner! You can also set the size of the buffer, or use the default size. In most cases, the default value is large enough.



scanner class provides multiple methods:
       next (): Gets a string;
      nextint (): Converts the obtained string to an integer of type int;
      nextfloat (): Converts the obtained string into a float type;
      nextboolean (): Converts the obtained string to a Boolean type;

   scanner class is in Java.util package, add import java.util.Scanner;   using scanner to get the user's input is very convenient, but scanner gets input based on a space character, including the SPACEBAR, tab, and enter. When you press either of these keys, scanner returns the next input. When you enter a space in the middle of the content, Obviously, you can't get the string you entered in full with scanner. At this point we can consider using the BufferedReader class to get input. In fact, in Java SE 1.4 and previous versions, there is no scanner method available, We also use Bufferreader when we get input.


The BufferedReader class is located in the Java.io package, so to use this class, you will introduce java.io this package

Import Java.io.BufferedReader. The ReadLine () method returns all character input before the user presses the ENTER key, excluding the last pressed enter return character. The ReadLine () method using the BufferedReader object must handle the java.io.IOException exception (Exception). Using BufferedReader to get input is much more complicated to understand. But using this method is fixed, and it can be done before each use.

Comparison of BufferedReader and scanner in Java

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.