Reading input from the console

In training materials from ElementK, I observed the use of System.in.read() to accept input from the keyboard. In my intro to Java class, I was taught to use the Scanner class in Java. What is the different between these two methods?

I’ll talk about the Scanner class although the Java API docs do a much better job of clarification. If you want to create a simple Java program where the user is prompted to enter words, characters or numbers, you need a class that allows you to do this. The Scanner class does not belong to the java.lang package to you need to import the Scanner class from java.util.Scanner using the import command at the top of your Java class.

import java.util.Scanner;

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s