Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Buffered input? (http://www.programmingforums.org/showthread.php?t=14298)

truBlu Oct 29th, 2007 11:26 PM

Buffered input?
 
Do I need the following lines of code when I want to input whole lines of text? If so, why do I need them? I found this code on the internet but I'm unsure of the purpose behind it. I tried google searching for the answer but came up short.

:

InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(converter);           
String word = input.readLine();


titaniumdecoy Oct 29th, 2007 11:53 PM

Re: Buffered input?
 
That is one way to read standard input. A better way is to use a Scanner:

:

Scanner sc = new Scanner(System.in);
You can use the nextLine() method to read an entire line, or next() to read the next word, as well as a number of other methods such as readInt().

Grich Oct 30th, 2007 12:56 AM

Re: Buffered input?
 
Also, remember to import the scanner class first.
:

import java.util.Scanner;

truBlu Oct 30th, 2007 1:27 AM

Re: Buffered input?
 
Thanks guys. But what is the purpose of buffered input? Why is there a need to use those lines of code when you use the readLine() function?

Grich Oct 30th, 2007 9:52 PM

Re: Buffered input?
 
It leads to more efficient programming apparently. It stores collected input output into temporary storage area. For example instead of reading each key stroke, it is collected into a buffer until the enter key is pressed.
When you use the buffer, it is using a class. You have to write code to declare it and use it, it is a bit weird at first to get used to JAVA and classes, but you get used to it.

~s.o.s~ Oct 31st, 2007 9:56 PM

Re: Buffered input?
 
> Why is there a need to use those lines of code when you use the readLine() function?
Scanner class was introduced in JDK 1.5 as a utility class which would serve as a golden hammer when doing I/O. Prior to that, if you wanted to accept user input via console, you had to use the BufferedReader, the way it is specified in the second post.

DaWei Nov 1st, 2007 12:44 AM

Re: Buffered input?
 
Build a piece of hardware and make it amenable to people who don't understand the bottom line. What else can I say?


All times are GMT -5. The time now is 3:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC