File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Bin Qian
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked the length of the string you are creating from the byte buffer?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read() is going to return any newline characters you type; does that help?
 
Aditya Kumar
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK,i know!
Thank you everyone!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic