This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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 System.in.read().. But I really don't know what else to do after that... Can anyone help? Maybe show me some sample code? Thanks in advance.
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. HTH Layne
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.
Joined: Apr 10, 2003
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.
Howdy 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."