How Do You Write a Java Program to Accept Text from the Command Terminal and Print It Back Out?

Problem scenario
You want to write a basic Java program to test it out.  How do you write a program to accept user input, specifically a alphanumeric input, and print it back to the screen?

Solution
1.  Install the Java Development Kit if you have not installed it yet.  If you are not sure you have it, run "javac -version".  If it says that the command is not found, you need to install the Java Development Kit.  If you do not know how, see this posting.

2.  Create this file named acceptText.java:

import java.util.Scanner;

public class acceptText {

public static void main(String[] args) {
   System.out.println("Please enter text, then press enter: ");
   Scanner x = new Scanner(System.in);
   String var1 = x.next();
   System.out.println(var1);
  }
 }

3.  Run this command: javac acceptText.java

4.  Run this command: java acceptText

Leave a comment

Your email address will not be published. Required fields are marked *