Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Console User Input?

 
Ernesto Pye
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernesto Pye
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so you know, I already switched my Display Name to my Real Name.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernesto Pye
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick search using google gives me this hit
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."

cheers,
Kathy
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic