• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Keyboard Input

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made a simple program to read from the keyboard



The problem is that when I write null on the console it doesn't terminate the program even though thats what i am using in the while condition.
Thanks
 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


br.readLine() returns the contents of a line, not including any line termination chracters or null if the end of the stream has reached.

so, when you type at the console "null", it is not the null object but it is the string "null".
The String "nul" is not a null object.
null means something that doesn't exist.

Hope it helps.
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mishra Anshu:


br.readLine() returns the contents of a line, not including any line termination chracters or null if the end of the stream has reached.

so, when you type at the console "null", it is not the null object but it is the string "null".
The String "nul" is not a null object.
null means something that doesn't exist.

Hope it helps.



Thanks for replying.
In that case shouldn't this work

while((val = br.readLine()) != "null") {
System.out.println(val);

But it doesn't work.
Even tried with something like
while((val = br.readLine()) != "stop") {
System.out.println(val);

and then wrote "stop" on the console (without quotes), but it doesn't work.
 
Mishra Anshu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what you want to do perhaps:--


Please see how two strings are compared.
Is it OK now?
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mishra Anshu:
This is what you want to do perhaps:--


Please see how two strings are compared.
Is it OK now?



THanks a lot
 
Mishra Anshu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if typing Ctrl-Z gives you a null on that readln(). Let us know.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic