aspose file tools*
The moose likes Java in General and the fly likes Please help me with this scanner object problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please help me with this scanner object problem" Watch "Please help me with this scanner object problem" New topic
Author

Please help me with this scanner object problem

Deepak Rajendran
Greenhorn

Joined: Aug 26, 2010
Posts: 16
The following program is not showing error. it is compiling smoothly.
but if i give



then the program is not responding after entering the trans type
but if i give as



then its working perfectly...

please explain what error i have made and why its happening like this

THANKS IN ADVANCE


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
My guess - The Scanner.nextLine() method is used after a Scanner.nextInt() method used to read the transaction amount. Say if you enter the transaction amount as 1000 and enter a return key (Enter key) the nextInt() method separates the 1000 and the "\n" return key input. Now it assigns the 1000 to the ta variable and leaves the "\n". From the Scanner API docs
Since this method continues to search through the input looking for a line separator
the nextLine() reads the line separator ("\n") left behind by the nextInt() method. And since there is no String before the "\n" it just assigns the empty String to the tans variable.

A way to prevent this to add a empty nextLine() reader that reads and leaves the left behind line separator. Or better use the next() method.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Harsha Smith wrote: TimeUnit.SECONDS.sleep(5);

I added that and gave the transaction as "deposit" but the code dint work further...
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Okay Harsha... Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
John Jai wrote:My guess . . .
Why “guess”? That is obviously correct.

Deepak Rajendran, please indent your code; it is very difficult to read otherwise.
Deepak Rajendran
Greenhorn

Joined: Aug 26, 2010
Posts: 16
John Jai : Thank you very much for your explanation. i am new to "scanner" so please do explain little more. see, usually when we get inputs using sc.nextLine() we have to give enter 2 times. why ? please explain that also.

Harsha Smith : Thank you for your reply. can you please explain why i have to use



Campbell Ritchie: sorry, surely next time i will



John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Deepak Rajendran wrote: usually when we get inputs using sc.nextLine() we have to give enter 2 times.

Why you have to give two times enter? Try below simple code... I always gave one time Enter!

A good read on Scanner.nextLine() method - Ranch old thread
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
You don't have to use actually......that was just a try


Happy Dussehra!
Deepak Rajendran
Greenhorn

Joined: Aug 26, 2010
Posts: 16
Hi John Jai i used your code and this time also 2 enter only works. if i give single enter its just going to the next line. then after second enter it prints the value !!!
if i use sc.next() instead, then single enter itself is working !!!
Deepak Rajendran
Greenhorn

Joined: Aug 26, 2010
Posts: 16
ok thank you Harsha Smith !!!
Happy Dussehra to you too
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Deepak Rajendran wrote:and this time also 2 enter only works

I don't know why only two enters work for you... Did you try running the class from the cmd prompt?
Deepak Rajendran
Greenhorn

Joined: Aug 26, 2010
Posts: 16
ya John Jai now only i tried in cmd prompt its working there.
But before, i used RAD it needs 2 enters every time. don't know why !!!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Comment the below line and try again in your RAD. I fear it's just an illusion that the cursor stays in the first line on the RAD's console..

Deepak Rajendran
Greenhorn

Joined: Aug 26, 2010
Posts: 16
John jai: yes i know that too, cursor staying in the printed line is just an illusion in RAD
but
after i type the input say "java" for example and then i press the enter(1st time)
the cursor moves to the new line
and after i press the second enter it is printing the string !!!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Okay... I give up
Deepak Rajendran
Greenhorn

Joined: Aug 26, 2010
Posts: 16
ya me too

ok anyway thanks so much for your detailed help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Harsha Smith, why have you deleted your earlier posts? Now the replies to them look like nonsense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me with this scanner object problem