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.
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.
posted 1 year ago
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.