• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

read input from console question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic