File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Console User Input? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Console User Input?" Watch "Console User Input?" New topic

Console User Input?

Ernesto Pye

Joined: Apr 10, 2003
Posts: 13
Hi, I'm new to Java. I'm already familiar with the Hello World favourite, System.out.Println()... I was just wonder how to get user input in the command prompt? I tried But I really don't know what else to do after that... Can anyone help? Maybe show me some sample code?
Thanks in advance.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Actually, I'm not sure how to do this. I'm definitely not a Java expert, but I started learning the language 4 years ago. Console I/O is somewhat complicated in Java. I would suggest that you skip this for now and move on to some of the basic concepts like if statements and for loops. If you understand those, then you need to work with classes and methods. There are plenty of things to explore in the language without using Console I/O. In fact, you can quickly learn how to create a GUI that provides a method of input that is a bit more pleasing to the eye.

Java API Documentation
The Java Tutorial
Ernesto Pye

Joined: Apr 10, 2003
Posts: 13
Oh, I see. Thanks for the information. Yeah, onto classes and methods I go. Any thoughts of what I should learn about after that?
Ernesto Pye

Joined: Apr 10, 2003
Posts: 13
Just so you know, I already switched my Display Name to my Real Name.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Originally posted by Layne Lund:
In fact, you can quickly learn how to create a GUI that provides a method of input that is a bit more pleasing to the eye.

Actually, I tend to think that learning to create GUIs is much more complicated than simple command-line input. When learning Java/programming in general, it is much easier to do quick tests from a command line than to wait until learning AWT/Swing before getting a method of input. As such, you can feel free to use this class I wrote when I was starting to learn Java. It is admittedly not overly-well documented, but the methods are fairly self-explanatory. (It shows the work of an inexperienced programmer; I don't use this overly much anymore, so I don't tend to maintain it).
I commented out the package declaration; you can add it to whatever package you wish.
Hope that this helps you and anybody else that might want to use this.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ernesto Pye

Joined: Apr 10, 2003
Posts: 13
Thanks a lot for that. I'm definitely keeping that in a text file, though I think I should first learn more of the basics before moving on to using the more complicated stuff.
Thanks again.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

A quick search using google gives me this hit

Please ignore post, I have no idea what I am talking about.
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
Here's an *extremely* simple example of getting command-line input. It's a static method, and you pass it a String (representing what you want to say to the user at the command-line, like, "Type a number between 1 and 10..." and it returns back the String the user typed in. It isn't robust or anything, but it works. We use this for our students, when they are writing game code in the early stages of learning Java, but aren't yet ready to work on I/O or even exceptions. In other words, we tell them to just "type this and use it, and later you'll learn how it works and how to modify it."

Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
you can use the following code to input something in console

I hope that I can help you to do less primitive data type convertion

Francis Siu
I agree. Here's the link:
subject: Console User Input?
jQuery in Action, 3rd edition