![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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(); |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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); |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1
![]() |
Re: Buffered input?
Also, remember to import the scanner class first.
import java.util.Scanner;
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#4 |
|
Newbie
|
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?
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1
![]() |
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.
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#6 |
|
Megalomaniac
Join Date: Nov 2006
Posts: 8
Rep Power: 0
![]() |
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.
__________________
"Be and not seem." |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Clearing the input box | 357mag | Java | 5 | Nov 1st, 2007 9:28 PM |
| Reading character input into an array (raw mode) | shoeyfighter | C | 3 | Nov 2nd, 2006 3:49 PM |
| User input | jayme | C++ | 38 | Nov 25th, 2005 7:27 PM |
| Checking form input | Jerry | Java | 1 | Oct 31st, 2005 8:41 AM |