• 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

Continious user input

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am actually relatively new here and was hoping someone could give me advice. I have been making small simple programs for the past month and was hoping someone could give me an idea on how to keep a program running. With the JVM I type "java (name of program)" in order to run it, but once it is done with the program it exits. I've heard that the System.in.read is some sort of way to wait for the users input, but I don't know much about it. Any help is fine.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, and welcome to JavaRanch,

To learn more about command line input, you can take a look at the I/O from the Command Line trail (i.e. topic) from The Java Tutorial. It explains basic command line input and output (I/O).

In terms of a program ending, you are correct. Once all the processing has completed, it ends. For a program to continue or keep running, a part of the program would need to be in some sort of state of waiting for something to happen, such as user input via the command line or a GUI, receiving a request from another system (like a database or web server does), etc. To do this, an application needs to be multi-threaded. You can learn about that in the Java Tutorial as well in the Concurrency lesson.

In addition to the Java Tutorial, I can highly recommend the book Head First Java (more info us available here) to assist you in the learning process. If you search JavaRanch for Head First Java, you will find many people singing its praises. It's very different from most technology books. And you definitely come away with a good understanding of Java basics.

Best Regards...
[ July 21, 2008: Message edited by: Mark Vedder ]
 
Tyler Knecht
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have bought Head First Java and am currently reading it. It's very unconventional and I find that it does a good job of explaining the ideas, I just need to put them into practice. As of right now I am on the sixth chapter, so I haven't gotten into wait for a user input. Is it possible that you could give me an example of waiting for a users input?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, modifying an example from the Console I/O tutorial (which has several example programs), we can make this:



Here the program will wait until the user inputs their name, then prints the name, then exits.

We could make it so the program never exits (until the user types exit) by putting that in a loop:


Generally we would be waiting for input for a specific reason; not just so the program continues to run.

As an FYI, Head First Java doesn't talk about console based input. But it does teach you about multi-threading. And later in the book it shows you how to write a GUI program. They (typically) keep running until you click the close button.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p.s. The System.in.read() method you mention in your original post can also used to get command line input. In fact, in older versions of Java its all you could use. Java 1.5 added the Scanner class (among some others) that make working with console input a littler easier. The Console class was added in Java 6 (aka 1.6) to make such things much easier.
[ July 21, 2008: Message edited by: Mark Vedder ]
 
Tyler Knecht
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This awesome, thank you. This is exactly the help I needed. Thank you.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Needless to say, there are other ways to do it. If you are feeling brave, you can try going through this thread. Reading it backwards like a whodunnit might be quicker!
 
reply
    Bookmark Topic Watch Topic
  • New Topic