The standard input stream — the keyboard.
(Scanner objects can be connected to other data streams.)
import java.util.Scanner;
public 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 );
}
}
System.out.println("Enter the data:");
This calls the method println to print the characters "Enter the data:" to the monitor.
inData = scan.nextLine();
This uses thenextLine()method of the object referred to byscanto read a line of characters from the keyboard. AStringobject (referred to byinData) is created to contain the characters.
System.out.println("You entered:" + inData );
This first creates aStringby concatenating "You entered:" to characters frominData, then callsprintln()to print thatStringto the monitor.
When the program runs, can the user edit the input string (using backspace and other keyboard keys) before hitting "enter"?
(Try this, if you have a running program.)