• 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

Boolean not registering

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
   
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Missing 'break'.
 
Edward Mallia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic