Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 29th, 2007, 10:26 PM   #1
truBlu
Newbie
 
Join Date: Sep 2007
Location: New Jersey
Posts: 13
Rep Power: 0 truBlu is on a distinguished road
Send a message via MSN to truBlu
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();
truBlu is offline   Reply With Quote
Old Oct 29th, 2007, 10:53 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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().
titaniumdecoy is offline   Reply With Quote
Old Oct 29th, 2007, 11:56 PM   #3
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1 Grich is on a distinguished road
Re: Buffered input?

Also, remember to import the scanner class first.
import java.util.Scanner;
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Old Oct 30th, 2007, 12:27 AM   #4
truBlu
Newbie
 
Join Date: Sep 2007
Location: New Jersey
Posts: 13
Rep Power: 0 truBlu is on a distinguished road
Send a message via MSN to truBlu
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?
truBlu is offline   Reply With Quote
Old Oct 30th, 2007, 8:52 PM   #5
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 150
Rep Power: 1 Grich is on a distinguished road
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 ...
Grich is offline   Reply With Quote
Old Oct 31st, 2007, 8:56 PM   #6
~s.o.s~
Megalomaniac
 
~s.o.s~'s Avatar
 
Join Date: Nov 2006
Posts: 8
Rep Power: 0 ~s.o.s~ is on a distinguished road
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."
~s.o.s~ is offline   Reply With Quote
Old Oct 31st, 2007, 11:44 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:49 PM.

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