This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
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.
 
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • 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.
 
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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: 64473
225
  • 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:-
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • 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: 21741
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • 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!
 
You can't expect to wield supreme executive power just because
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!