• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting input from user which way is better ?  RSS feed

 
vamsi naki
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just wanted to know whats the difference between using


& using



Is it like one should be used in a particular type of scenario
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner is more recently‑developed code. It probably uses regular expressions to identify different sorts of token. You can avoid Exceptions from scanner entirely with the sort of loop in a utility class, as in that post. You can get different values, eg double, BigInteger, directly from the Scanner (but not chars) You can get problems with line end like here.
You may find a BufferedReader more powerful; I have had some files which BufferedReader read happily but Scanner struggled with. If you are using a BufferedWriter, you can append to a file, which I have never worked out how to do with a Formatter.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vamsi naki wrote:Is it like one should be used in a particular type of scenario

Personally, I always use nextLine()/readLine(), whether it's from a Reader or a Scanner, because it forces the user to hit the RETURN key before anything gets processed. You can always set up another Scanner to process the resulting String.

But that's just my opinion; I'm sure there'll be others.

Winston

 
vamsi naki
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Campbell & Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!