Yes — Scanner
objects have many input methods.
Echo.java
import java.util.Scanner; class Echo { public static void main (String[] args) { String inData; Scanner scan = new Scanner( System.in ); System.out.println("Enter the data:"); inData = scan.nextLine(); System.out.println("You entered:" + inData ); } }
Here is the Java program. It reads characters from the keyboard and echos them to the monitor.
First a Scanner
object is created:
Scanner scan = new Scanner( System.in );
Then the user is prompted to type something in.
Next a method of the Scanner
is called:
inData = scan.nextLine();
This reads in one line of characters and creates a String
object to contain them.
A reference to this object is put into the reference variable inData
.
Next the characters from that String
are sent to the monitor:
System.out.println("You entered:" + inData );
The line import java.util.Scanner;
says to use the Scanner
class from the package java.util
.
Here is a run of the program:
Could the user include digits like 123 in the input characters?