• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me with this scanner object problem

 
Deepak Rajendran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Harsha... Thank you!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to use actually......that was just a try


Happy Dussehra!
 
Deepak Rajendran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank you Harsha Smith !!!
Happy Dussehra to you too
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay... I give up
 
Deepak Rajendran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya me too

ok anyway thanks so much for your detailed help
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith, why have you deleted your earlier posts? Now the replies to them look like nonsense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic