-6

Possible Duplicate:
Scanner vs. BufferedReader

what is the difference between using the statements shown below:

Scanner input = new Scanner(System.in);
int number = input.nextInt();

and

InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
int number = input.readLine();

thank you in advance for your help.

Community
  • 1
  • 1
Sinan
  • 453
  • 2
  • 10
  • 20

2 Answers2

1

suppose in the consol, you entered

10 20 

and then press ENTER key

The first case will return 10 but the second case will not compile, because readLine() returns String not int.

In case if you try to parse that returned String to int, then also it gives NumberFormatException, as it reads the complete line, which contains two numbers along with a WHITE SPACE

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
1

The first compiles, the second not.

A scanner will parse the input to a datatype, the BufferedReader will give the data line by line as it is.

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79