Yes. (Although frequently those characters are converted to a numeric type after they have been read in.)
Later on we will do something special to convert strings of digits into primitive numeric types. This program does not do that. Here is the program again:
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 );
}
}
class Echo
The program defines a class namedEchothat contains a single method, itsmain()method.
public static void main ( String[] args )
Allmain()methods start this way. Every program should have amain()method.
String inData;
The program will create aStringobject referred to byinData.
Scanner scan = new Scanner( System.in );
This creates aScannerobject, referred to by the identifierscan.
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.
Can the user edit the input string (using backspace and other keyboard keys) before hitting "enter"?