how to create a simple console menu java tut

when you first learn java you start with console applications, outputting text and getting simple user input. alot of java console based applications have a text menu where the user can chose options.

in this tutorial you will learn how to make a reusable console menu class.

2020 update:

if you really want to learn java take this course(the first part is free):

the best java course

if you would like heres a video version of this lesson:

in this tutorial i will show you how to create a simple java console menu.

for this tut we will use the eclipse ide, i assume that that is the ide you are using.

this is how the final product will look like in the java console:

first thing in eclipse create a new java project and call it somthing like console menu (the name dosnt matter).

after that create a package and call it helper(right click project src):

right click on the helpers package and add a new class called S:

copy this code to the S class :


package helpers;

public class S
{
	public static void o(Object s)
	{
		System.out.println(s);
	}

}

this is the code from this tutorial.

create a package called main and create a main class:

create a package called console and create a class called console_menu .

this is the code for console_menu :


package console;

import java.util.Scanner;

import helpers.S;

public class console_menu {

	public boolean is_running;
	public String[] menu_items;

	public console_menu()
	{
		is_running = true;
		menu_items = new String[]{"show all","add","edit"};
	}//end constructor

	public void run()
	{
		while(is_running)
		{
			menu_show() ;
			menu_user_input();
		}
	}

	public void menu_show()
	{
		int i=1;//iterator
		//foreach menu_items
		for(String item :menu_items)
		{
			S.o(i+") "+item);
			i++;
		}
	}//end menu_show

	public void menu_user_input()
	{
		//get user input
		Scanner user_input = new Scanner( System.in );
		String next = user_input.next();
		//if user entered 1
		if(next.equals("1"))
		{
			S.o("you chose show all \n");
		}
		//if user entered 2 etc etc
		if(next.equals("2"))
		{
			S.o("you chose add \n");
		}

		if(next.equals("3"))
		{
			S.o("you chose edit \n");
		}

		//exit program if user writes exit
		if(next.equals("exit"))
		{
			S.o("program ended");
			is_running = false;
		}

	}//end menu_user_input

}

at the top of the class we declare two attributes a boolean and a string array:


public class console_menu {

	public boolean is_running;
	public String[] menu_items;

the string array will hold all the menu items and the is running boolean will determine if the program will keep on running.

in the class constructor we set is running to true and instantiate the menu_items array and give it some elements:


	public console_menu()
	{
		is_running = true;
		menu_items = new String[]{"show all","add","edit"};
	}//end constructor

the run method is what runs the console menu program:


	public void run()
	{
		while(is_running)
		{
			menu_show() ;
			menu_user_input();
		}
	}

basically what it dose is while is_runing is true show the menu and wait for user input.

what menu show method dose is to loop the menu item array and show them numbered:

	public void menu_show()
	{
		int i=1;//iterator
		//foreach menu_items
		for(String item :menu_items)
		{
			S.o(i+") "+item);
			i++;
		}
	}//end menu_show

the loop is a foreach loop. for each menu items it outputs the current i value and the current string item.after that it increments i .

the menu_user_input method uses a scanner object to get the user input and after that it determines using ifs what to output to the user:

	public void menu_user_input()
	{
		//get user input
		Scanner user_input = new Scanner( System.in );
		String next = user_input.next();
		//if user entered 1
		if(next.equals("1"))
		{
			S.o("you chose show all \n");
		}
		//if user entered 2 etc etc
		if(next.equals("2"))
		{
			S.o("you chose add \n");
		}

		if(next.equals("3"))
		{
			S.o("you chose edit \n");
		}

		//exit program if user writes exit
		if(next.equals("exit"))
		{
			S.o("program ended");
			is_running = false;
		}

	}//end menu_user_input

if the user writes exit the is_running boolean is set to false and the loop inside run stops.

finally we go to the main class in main package and create a console menu object and run it using the run method we created:

package main;

import console.console_menu;
import helpers.S;

public class ymain {

	public static void main(String[] args) {
		//S.o("test");
		console_menu cm = new console_menu();
		cm.run();
	}

}

2 thoughts on “how to create a simple console menu java tut

Leave a comment