Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Keyboard Input  RSS feed

 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
 
Mishra Anshu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if typing Ctrl-Z gives you a null on that readln(). Let us know.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!