Echo.java

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:

Yes — Scanner objects have many input methods.

Echo.java

Here is the actual Java program. It reads characters from the keyboard and creates a String object to contain them. A reference to the object is put in inData. Then it sends the characters from that String to the monitor.

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

The line  import java.util.Scanner;  says to use the Scanner class from the package java.util. The java.io package is imported automatically when you import Scanner. Here is a run of the program:

run of the echo program

It would be beneficial to your future endeavors if you create a source program, compile, and run it.

QUESTION 7:

Could the user include digits like 123 in the input characters?