Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Switch Statement

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following switch statement and I need to end the program if the user select number 2.

When the user select the number 2, the program still runing, but it needs to terminate.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry... i'm not very good in java too... but maybe u can use System.exit(0); instead of break... and the case 2 should be default... as it is the last case...
 
Marving Guildon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it works.
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, System.exit(0) would end the program. But...
I wouldn't make 2 the default case. If you want the user to exit when explicitly selecting '2', then making all but '1' the default case would mean the program would exit if the user selected '3', or '4', or any value other than '1', which seems to go against what you wanted to do.
Anywho, that's my last 2 cents for today.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that System.exit() should rarely be used to end your program. A better way is to ensure that all your threads have finished (in a single-threaded program, this just means allowing main() to return).
There are a number of reasons for this: -
  • System.exit() causes an immediate exit. It does not take account of the state of other threads; they will all be forced to exit immediately.
  • A method with System.exit() is more difficult to re-use in another application.
  • The compiler does not realise that flow cannot go beyond a System.exit() call, and hence its ability to check whether you have unreachable code, missing "return", or suchlike, is compromised.

  •  
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In addition, as a point of enlightenment, all "break" does is literally break out of the switch block... it has nothing to do with actually terminating your program. You can also use it in loops and so forth.
     
    If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic