This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to use Runtime.getRuntime().halt(0);?

 
Greenhorn
Posts: 1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a programm where the user should guess a number from 1 to 100 (the number gets randomly generated). I wanted to use the Runtime.getRuntime().halt(0); command to stop the loop after the user guessed the correct number.
 
Marshal
Posts: 6944
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Emilie de Rochefort wrote:I wanted to use the Runtime.getRuntime().halt(0); command to stop the loop after the user guessed the correct number.


That is sort of abnormal way of stopping something when the condition has met.

Think if you could have something similar to:

You also would need to think how to enter loop.
 
Saloon Keeper
Posts: 6035
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have several things wrong with your code.
1) Instead of halt(0) use System.exit(0).
2) Your call to exit() should be inside an if(){...} block.
3) Instead of halt() or exit() re-write your loop to be a do { ... } while( secret != enter );
4) Your random values will only be 1 to 99 (inclusive), not 1 to 100.
 
Marshal
Posts: 65019
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Emilie de Rochefort wrote:. . . . I wanted to use the Runtime.getRuntime().halt(0); command . . .

Why? If you look up Runtime#halt() you will see that it warns you to be very careful. Methods like that and System#exit() stop the JVM abruptly and can under some circumstances stop processes you should keep running, risking corrupted records or incorrect results. It is probably a good idea to avoid both those methods altogether.
Also, you have the halt() call in the wrong place. As Liutauras and Carey have told you, it is much better to write a loop that will terminate when it completes its task.
I don't like arithmetic with Math#random(), despite what it says in the Java™ Tutorials. I would prefer to use a Random object:-
 
Sheriff
Posts: 6108
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using try as a variable, but "try" is a Java keyword so that code shouldn't even compile.  Use some other variable name.
 
Sheriff
Posts: 21774
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Runtime.getRuntime().halt(0) to stop a loop is like using a bazooka to open your front door.... It does the trick, but it basically destroys everything around it.
 
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Using Runtime.getRuntime().halt(0) to stop a loop is like using a bazooka to open your front door.... It does the trick, but it basically destroys everything around it.



Yeah. Have you considered just calling in a nuclear strike instead?

Fun fact.

The first computer I ever got to play with was an IBM 1130 system. They didn't call it a minicomputer (that term came later), but as mainframes went it was quite small.

This was not a multi-tasking system, and in fact, didn't technically have an OS as such - you just loaded in punched cards and ran. But somehow the engineers had finagled it into being able to compile and run Fortran programs while concurrently doing real-time process control at a chemical plant 300 miles across the state.

The one thing we were cautioned against was not to use the STOP statement to terminate our programs. Because if we did, it would reset the whole system and shutdown that chemical plant!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!