Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Taking input with Scanner : need to exit on pressing enter key

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to take strings from the user and stop taking input when :

1-he enters nothing and presses only enter. OR if this is not possible,
2-he enters "exit".

What is the code to be put inside the while loop below for the above cases to happen ?
I tried has next , has next line , !has next("exit") and failed. Please tell me how to proceed.

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your program needs to run forever but it needs to stop when the user either enters nothing or exit. Hint: break.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the revised program. User can exit upon entering "pl exit".But one problem remains how to exit if he presses enter key twice, without entering anything else ?



thanks for the hint.
rb

PS : here is another problem i had long ago while using the Scanner class :
http://www.coderanch.com/t/528543/java/java/Taking-input-keyboard-takes-ints#2397967
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like break; myself. The idiom for that sort of thing in the heading of a while loop is not the sort of thing you can work out unless you have seen it. Try this sort of thingYou can add a second test with the && operator, but don't call nextLine() twice.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I don't like break; myself. The idiom for that sort of thing in the heading of a while loop is not the sort of thing you can work out unless you have seen it. Try this sort of thingYou can add a second test with the && operator, but don't call nextLine() twice.


Love you man... . By the way, what is an "idiom" saw it in the sun docs too, just ignored it and there was no loss of continuity.
Here is the revised program :



sample output :

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your help has made another program possible. I use the above code in the program below :



This code works fine if you enter each sentence word by word (as shown below) :



But it obviously fails when the input is given as below :


How to resolve the above problem. Will it require major rewriting of code ?

thanks for hearing
rb
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote: . . . Will it require major rewriting of code ? . . .
Yes. You would have to change nextLine() to next()

You might have to enter ctrl-D or ctrl-Z to terminate entry if you use that technique, and use hasNext() to continue with the loop.

And an idiom is something that is said in a particular context, which would otherwise appear incorrect. It is a linguistic term. An example of an English idiom is "raining cats and dogs".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic