File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Taking input with Scanner : need to exit on pressing enter key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Taking input with Scanner : need to exit on pressing enter key" Watch "Taking input with Scanner : need to exit on pressing enter key" New topic
Author

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

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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.



SCJP 6. Learning more now.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

So your program needs to run forever but it needs to stop when the user either enters nothing or exit. Hint: break.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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

Joined: Oct 13, 2005
Posts: 37976
    
  22
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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Oct 13, 2005
Posts: 37976
    
  22
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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Taking input with Scanner : need to exit on pressing enter key
 
Similar Threads
Binary to Decimal conversion
Java noob -- sentinel-controlled while loop
IOException
Help with parsing integers from a string
Write to a txt. file, via user input? (java with netbeans)