Forums Register Login

console input

+Pie Number of slices to send: Send
can anybody show me an example of code to input a string from the console window. I can take an argument to the program call as input, but I don't know how to get input once the program has started. I have a book that mentions creating a Scanner object and using it's next() method with System.in to get the next string. I can't get my programs to recognize Scanner though. I'm using the newest j2sdk1.4.2_12. any suggestions?
+Pie Number of slices to send: Send
Hi Glen,

Welcome to JavaRanch!

Scanner is a feature of JDK 1.5, which is now actually a few years old; JDK 1.4 is old-timer stuff by now. 1.6 is actually in late beta!

If you're stuck with 1.4 and can't upgrade for some reason (but you should, you really should...) then you can use BufferedReader to read lines of input. If you need to read numbers, you'll have to parse them out by hand, which is also a pain. But reading a line of text looks something like



That "1" in the first line is quite important, but many books forget to tell you about it. Without it, your program may appear to "hang" occasionally for reasons that we don't need to go into right now.
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 420 times.
Similar Threads
Question about jGrasp
Ways Of Getting input in JAVA 6 ?
StringBuffer
Having issues with sub menu causing main menu to not exit
Best way to get user input
Thread Boost feature
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:09:05.