Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading Input from Keyboard

 
Benjamin Chau
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic