• Post Reply Bookmark Topic Watch Topic
  • New Topic

a doubt regarding a simple program using the Scanner class  RSS feed

 
achuthan mukundarajan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a simple program where a user is asked for his name , his preferred discount rate and also the cost of his bill. The discount is calculated and the end statement shows all the above variables. The code is written inside a while loop so that the user can repeat the same operation even after the first result.
The code is given below:


The above program works fine. But if give the scanner object creation outside the while loop , then when i go for the second attempt , the cursor does not go for line 15 i.e the name is not accepted and it goes to line 17. Why doesnt the compiler work for the name part if the scanner is declared outside the while loop?Thanks in advance
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I applaud your using code tags in what is at most your third post, it would really help if you also properly indented your code. Making it easy for people to read will make a big difference in who is willing to help you.

Can you try editing your post to do that?

Thanks!!!
 
achuthan mukundarajan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the solution . I used sc.next() instead of sc.nextline(). By the way fred there is no need to applaud me for using code tags , i have posted quite a lot on the asp.net forums before.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!