Details (continued)

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

The standard input stream — the keyboard.

(Scanner objects can be connected to other data streams.)

Details (continued)

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 );
  }
}

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 the nextLine() method of the object referred to by scan to read a line of characters from the keyboard. A String object (referred to by inData) is created to contain the characters.

System.out.println("You entered:" + inData );

This first creates a String by concatenating "You entered:" to characters from inData, then calls println() to print that String to the monitor.

QUESTION 10:

Can the user edit the input string (using backspace and other keyboard keys) before hitting "enter"?