• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a doubt regarding a simple program using the Scanner class

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic