aspose file tools*
The moose likes Beginning Java and the fly likes Reading Input from Keyboard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading Input from Keyboard" Watch "Reading Input from Keyboard" New topic
Author

Reading Input from Keyboard

Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
I actually found some threads on this subject the other day but I can't find them again now when I do a search. I'm needing a more thorough understanding of writing statements to make a Java program get basic input from a user. For example, if my output statement is:

System.out.println("Please enter your telephone number");

How do I get the user's data when he inputs it? From what I gather so far, methods of reading input vary according to the situation and need. One thing (I think) may work is using the statement:

Scanner sc = new Scanner(System.in);

to instantiate a Scanner class, then nameing it sc, and wrap it around stdin. Then the statement:

Scanner sc = sc.nextInt(); reads the telephone number the user inputs using his keyboard. Since an integer number will be entered, the type is int.

Any thoughts anyone???
Bharadwaj Adepu
Ranch Hand

Joined: Dec 30, 2007
Posts: 99
Rather you can use BufferedReader.


The Phone number will be stored in the above string and you can convert it to Integer if you want.


SCJP 1.5
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
Actually Scanner is the more uptodate way of reading input, but was only introduced in Java 5. If you are happy to restrict use of your program to Java 5 or above JVMs then use Scanner, otherwise use BufferredReader.


Joanne
 
 
subject: Reading Input from Keyboard