This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Boolean not registering

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a vending machine program and in the fillOne procedure before asking if you want to refill another object it should check if the vending machine is actually empty.
for some reason its setting the boolean empty to true and you end up stuck in a loop because all the items are full.
I dont know if i have a nested loop or conflicting variables maybe?
Sorry the code is so long but im not sure what you'll need for reference.



 
   
 
Bartender
Posts: 7302
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Carey Brown
Bartender
Posts: 7302
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Edward Mallia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly does the "break" do as in my first switch statement case 3 does not have the break but it still works
Thanks for the reply, will test it out and get back to you shortly
 
Marshal
Posts: 70305
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Edward Mallia wrote:What exactly does the "break" do . . .

It exits the switch statement. Always write break; after every case XXX: or default: unless you actually want fall‑through. In which case wite a comment saying the fall‑through is intentional.
The reason option 3 works is that you are exiting the program altogether. I don't think it is a good idea to use System.exit(). It might not cause any problems in a simple app like yours, but as soon as you start multi‑threading, System.exit() can do no end of harm because it can terminate programs which haven't finished their tasks.
Only use \n and \r if somebody has told you they want a particular line end. If you need empty lines before the start of the printout, write System.out.printf("%nOne line%nSecond line%n"); or similar.
Your methods are much too long; they should be divided into much smaller methods.
Remove the keyword static from your vocabulary. That takes you out of the realms of object‑oriented programming. You should have written a VendingMachine class, a Product class, and probably a few more classes.
 
Edward Mallia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks, I thought it had something to do with the fact that i was exiting the program.

I do realize my code is very long but I'm 15 and have been coding for not more than 2 years so i don't really know any shortcuts or any other tricks.

I did have an item class i just didnt attach it here and i did just realize i should have done the "initialising" in that class.

Also to be honest i dont know what that static means it was just how we did it in class. The teacher never explained in detail what it does as we're still beginning and so i didn't try anything different as removing the static creates an error and i dont know any alternatives.

I didnt really understand what you were trying to say about the line ends and \n and \r but thanks for all the tips.
    Bookmark Topic Watch Topic
  • New Topic