-4

I have been learning java since last two months and I am new to Stackoverflow as well. In my class, I was told that there are 7 methods by which we can take input from the user and read it (or do calculation with it or whatever). But, they taught us only one, that was using inputStreamReader. But as much as I have understood through Stackoverflow, using Scanner class is a general practice. I want to know which method is the best one, and if it really makes any difference as to which method we use.

Thanks in advance for help.

Ashima
  • 129
  • 1
  • 11
  • 3
    That depends on what you need to do... – MadProgrammer Jul 31 '13 at 06:15
  • This is what I want to ask, when should i use Scanner and when should I use inputStreamReader. Like, is there something that could be done by Scanner but could not be achieved through inputStreamReader? – Ashima Jul 31 '13 at 06:16
  • Did you thouroughly look for an answer before asking? Check this post http://stackoverflow.com/questions/10067465/scanner-vs-inputstreamreader – wea Jul 31 '13 at 06:21
  • -1 you should be asking specific questions for specific problems, that have a real answer. – MrFox Jul 31 '13 at 06:24
  • I have checked this post @wea, but my question is not limited to just scanner or inputStreamReader, that is just an example, I want to know a method that is good for all situations, or actually, most situations. – Ashima Jul 31 '13 at 06:24

5 Answers5

2

I typically prefer to use the Scanner class to read from the input line.

With the Scanner class you are able to request specific types (double, int, ..., string). This will do validation testing for you as well.

Aurelius
  • 11,111
  • 3
  • 52
  • 69
MayurB
  • 3,609
  • 2
  • 21
  • 36
0

Dialogs are nice too. http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html look at "Getting the User's Input from a Dialog".

MGorgon
  • 2,547
  • 23
  • 41
  • I know dialogs are nice and I know that practice of JOptionPane and all. But I think dialogs are generally used when we are designing a GUI based application. When I need to interact with the user through CUI only, I dont think dialogs would be a nice approach, isn't it? – Ashima Jul 31 '13 at 06:21
0
I want to know which method is the best one?

No one can answer this generic question for you! You have to use what best suits your requirement.

Here is the link to all the I/O methods used in Java.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

System.in is what you will take input from standard input device ie keyboard. rest all depends based on need, whether you would like to buffer it first or directly read from stream etc. Scanner class is helpful on top of taking input as it has several methods for particular type of input to take in case of data type instead of casting to Int or Long etc.

Jayesh
  • 6,047
  • 13
  • 49
  • 81
0

Scanner , InputStreamReader etc can be used . and the best way depends on your requirement .

Shuhail Kadavath
  • 448
  • 3
  • 13