Reputation: 8414
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
Reputation: 298818
The Charva Project may be what you are looking for. It's a Command-Line based GUI framework.
Upvotes: 2
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
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