Skip to main content
gamedev.net
🔒 Locked

[java] read() vs readLine() question

Started by Baine 3 replies 4.4k views
Original Post
Baine
Baine
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 = stdin.read(); 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
groovy,ern
thurk
thurk
Im by no means on expert on IO especially in java, but try this:

        
try{
String file = "";

//InputStream stdin = System.in;

BufferedReader fileIn = new BufferedReader(new InputStreamReader(System.in));

//Get the Yes or No Value

int yesOrNo = 0;

while(true){
yesOrNo = fileIn.read();

if(yesOrNo == 121 || yesOrNo == 110) break;
System.out.println("
y/n");
}


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

//System.out.flush();


//Get the Filename

while(true){
file = fileIn.readLine();
if(file.length() > 0) break;
}

System.out.println("
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

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

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

Edited by - Thurk on November 20, 2001 10:31:10 AM
WayfarerX
WayfarerX
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. InputStream.read() 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
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Baine
Baine
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


groovy,
ern
groovy,ern

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.