---
# System prepended metadata

title: A simple command-line interpreter

---

# A simple command-line interpreter

## Initialize a new project
- Create a new project named `java-simple-cli`
- Create and edit a new file `Cli.java`
- Declare a public class named `Cli` with a `main` method
- Add a line of code in the `main` method to print Test in the console
- Compile then run the Cli program, the output should be `Test`

## Add a scanner into the program
- Discover what the `Scanner` class is, [source](https://www.geeksforgeeks.org/scanner-class-in-java/), do not implement the examples in the source, simply read and try to understand
- In the source class, replace current code by the following lines of code:
```java
import java.util.Scanner;

public class Cli {

    public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in); // Listen to the standard input (console)
		System.out.print("> "); // Prompt
		while (true) { // Infinite loop
			String command = scanner.nextLine(); // Get input from console as a string
			String output = ""; // A variable named output of type String
			if (command.equals("exit")) {
				break; // Forces exit of the while loop
			} else {
				// String concatenation
				output = "Command '" + command + "' not found.";
			}
			System.out.println(output); // Print with new line (ln)
			System.out.print("> "); // Prompt
		}
		scanner.close(); // Best practice, always close a stream when no more needed
		System.out.println("Bye!");
    }

}
```
- Compile and test the program
- Understand what a package is in Java, what means the `import` keyword?
- Learn why we do not need import statements for the `System` and `String` classes?