Justin Kredible
Justin Kredible

Reputation: 8414

Command line Java program with menus

I need to create a command line Java program that has options on the screen to perform different tasks. For example, I want to have four options on the screen numbered 1 through 4. The user should be able to enter one of the numbers on the screen for the option they want, then press enter. They will then be taken to another screen which may have another menu. The user should also be able to navigate back to the main menu.

Any ideas?

Upvotes: 1

Views: 9125

Answers (3)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298818

The Charva Project may be what you are looking for. It's a Command-Line based GUI framework.

Upvotes: 2

AlexR
AlexR

Reputation: 115328

You can print options using all formatting facilities you know. To read user's input use System.in.read() or use classes Scanner and/or Console. Console even has ability to get non-echoed input (useful for passwords).

Upvotes: 0

c00kiemon5ter
c00kiemon5ter

Reputation: 17594

Here is some pseudocode; I think it's self explainatory.

mainmenu() {
    while (true) {
        printMainMenu();
        choice = readInt();   // make sure it's an int
        switch (choice) {
            case 0: exit();
            case 1: foo();
            case 2: bar();
            default: print("Wrong choice");
        }
    }
}

foo() {
    // same thing but instead of exit, break the while loop
}

you can model a class that would build a menu given the choices and reuse that.
but this ^ should be enough to get you started.

Upvotes: 3

Related Questions