• Post Reply Bookmark Topic Watch Topic
  • New Topic

read input from console question  RSS feed

 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following code to read user's input repeatedly until user types "n", but whatever I typed, the while condition is always true, can anyone tell me what's the problem of the code? Thanks.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change while((reply=br.readLine())!="n") to something use equals().
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, chi Lin.
I changed the original code to "while(!((reply=br.readLine()).equals("n")))" and it works, but could you explain why? Does readLine() method create a String object(in heap) from the console input and return it?
[ May 14, 2004: Message edited by: Claire Yang ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the object is not in literal pool, so compare with != don't give correct result.

to see this, try
while( (reply=br.readLine().intern()) != "n" )

but equals() should be a better approach.
[ May 14, 2004: Message edited by: chi Lin ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!