Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes I/O and Streams and the fly likes why it doesnt output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "why it doesnt output "Nice to meet you too!"?" Watch "why it doesnt output "Nice to meet you too!"?" New topic

why it doesnt output "Nice to meet you too!"?

Bin Qian

Joined: Sep 13, 2006
Posts: 18
Hello ,everyone!
this is my confusion:

No matter what i input,it just outputs that i have inputed!
Why when i input "Nice to meet you!",it is also output "Nice to meet you!" not "Nice to meet you too!"?

Please!thank you!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you checked the length of the string you are creating from the byte buffer?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

read() is going to return any newline characters you type; does that help?

[Jess in Action][AskingGoodQuestions]
Aditya Kumar
Ranch Hand

Joined: Oct 31, 2006
Posts: 102
When you press enter after feeding in from the console, two characters are concatenated to your already entered string -- the linefeed and the new line ('\r' and '\n').

That is why you will see that the length of the entered string is always more by 2 characters. If you enter one character and do a string.length(), you will see it prints 3.

A one line addition makes your code work. Before you compare the two strings, add this:

Hope it helped!
Bin Qian

Joined: Sep 13, 2006
Posts: 18
OK,i know!
Thank you everyone!
I agree. Here's the link:
subject: why it doesnt output "Nice to meet you too!"?
It's not a secret anymore!