Advertisement Jump to content


This topic is now archived and is closed to further replies.


[java] read() vs readLine() question

This topic is 6271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

howdy. I''ve come across a little snag in a current project of mine, and I was wondering if anyone could offer some insight as to why it is happening. I prompt the user with a question, requiring a y/n type answer. I use: selection =; to acquire the users selection. everything seems fine and dandy up to this point. immediately following this, I try to prompt the user for the name of a file: filename = stdin.readLine(); now here''s my problem. It seems as if there''s something left in the.. umm.. "input buffer" <- definately isn''t the right wording, but i''m not well-versed in this lingo yet. Anyway, the program seems to initialize filename to absolutely nothing, and jump to the next statement in my program without prompting the user to enter a filename. As far as I know, the problem lies in the read() statement that I used immediately before the readLine() statement. If i replace the beforementioned read() with a readLine() that performs the same task, the program works properly. What is it about the read() function that I''m missing? Any info would be greatly appreciated hmm.. also if anyone knows the procedure with respect to creating codeblocks within'' my posts on this forum, it''d be cool if ya let me know groovy, ern

Share this post

Link to post
Share on other sites
Im by no means on expert on IO especially in java, but try this:

String file = "";

//InputStream stdin =;

BufferedReader fileIn = new BufferedReader(new InputStreamReader(;

//Get the Yes or No Value

int yesOrNo = 0;

yesOrNo =;

if(yesOrNo == 121 || yesOrNo == 110) break;

System.out.println(yesOrNo == 121 ? "
Yes" : "No");
Getting the Filename...");


//Get the Filename

file = fileIn.readLine();
if(file.length() > 0) break;

FileName: " + file);

//FileReader fileReader = new FileReader(file);

catch(IOException ioe){
//Do Something with the IOExecption


Crude I know, but hey its seems to work.

Im actually waiting on WayfarerX's reply myself

The codeblock thingy is...
where $ource is source
I would have thought that would be easier to explain

Edited by - Thurk on November 20, 2001 10:29:01 AM

Edited by - Thurk on November 20, 2001 10:31:10 AM

Share this post

Link to post
Share on other sites
Wow, I'm famous. Don't count on me too much though, I've been kind of off the last couple of weeks (just ask c_wraith).

Here's what I think is happening. reads a single byte off the stream, so this would be your 'y' or 'n' character. From what I've seen though, the OS won't actually send the info typed by the user to your app until the user hits "Enter". This puts a newline after your 'y'/'n' character in the stream.
When you call readLine(), it finds the newline that's still sitting in the stream and returns, giving you an empty filename.

Use readLine() for both calls.

"So crucify the ego, before it's far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that's imagined and all conceivable."
- Tool

Edited by - wayfarerx on November 20, 2001 11:47:41 AM

Share this post

Link to post
Share on other sites
Yea I came across the exact same problem in my last project. I was grabbing data from a websource though. Wayfarer is right, if you use readLine() for your input streams there shouldn''t be any problems.

Share this post

Link to post
Share on other sites
Hey thanks for the suggestions

That newline thing makes sense, and it''s cool to know why things don''t work rather than just knowing a workaround. Anywho, I''ll use readLine() for both calls, and thanks a lot for the help


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!