Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to accept i/p's from user

 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to accept i/p's from user in a command prompt?

when we want to print data to standard o/p we use system.out.print(data)
..but how do we accept data from user and store it in a variable
/*system.in()...*/ is it? or is something to do with Handler class

i am beginner pls help!
Thank's
amal
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you mean by "ips" as inputs, not a users actual IP address.
You can get input from a user by using the "Scanner" class.

 
Ellie Barnes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are indeed looking for command line input, you could also do it this way:



You would need to provide some exception handling to go with this too, enclose your code in a try.......catch block.

Event handling which you mention is usually used to get input from a GUI (e.g. a user clicks a button), whereas this kind of system prompt input is not event driven (maybe it can be?)

Hope that's helpful


[ June 03, 2006: Message edited by: Ellie Barnes ]
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank's ellie that helped...stephen the scanner class don't seem to appear in java doc 1.4..well how to go about with this scanner class
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amal shah:
thank's ellie that helped...stephen the scanner class don't seem to appear in java doc 1.4..well how to go about with this scanner class

The java.util.Scanner was new with Java 1.5. So if you're using 1.4 or earlier, you should use the approach outlined by Ellie.

Or you can upgrade to Java 5.0.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can upgrade to Java 5.0.
Or indeed look forward to the new java.io.Console class in Java SE 6!
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could use Jakarta Commons CLI, also.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic