Forums Register Login

Continious user input

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
This awesome, thank you. This is exactly the help I needed. Thank you.
+Pie Number of slices to send: Send
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!
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 7186 times.
Similar Threads
Java Input
Java help, please!!!
Recursive Descent Parser Question
Java input
Arrays, For Loops,.....???
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:39:36.