Calling method from within itself?

ok so i have a method that displays a menu and returns the user selection.

public class Menu {


public static int menuSelect(){

Scanner input = new Scanner(System.in);

System.out.println("Hello, Please Select A Function: ");
System.out.println("1) Sort All Banks Alphabetically ");
System.out.println("2) Calculate Interest And Show Balance ");
System.out.println("3) Transfer Money ");
System.out.println("4) Calulate Sum & Average Of All Accounts: ");
System.out.println("5) Richest Account: ");
System.out.println("6) Poorest Account: ");

int select = input.nextInt();

Menu.menuSelect();

 //i tried this as adding Menu.menuSelect(); 
 //after the return threw an error.
 // but surprise suprise this doesnt actually 
//let the method return anythign to select

  return select;
}

The idea is that i want menu to come up the user selects a function, the function happens and then the menu calls itself until told otherwise. but im unsure how to do this. any help would be greatly appreciated.

Answers


Calling the same method from itself is called a recursion, and it's infinite in your case. You obviously don't need it here.

You want to have something like this:

private static int getInput() {
    int choice = menuSelect();
    while(choice < 1 || choice > 6) {
        System.out.println("Invalid choice, please re-enter")
        choice = menuSelect();
    }
    return choice;
}

Note that it's bad to give menuSelect a public modifier, you don't want anyone outside the class to have an access to it.


Need Your Help

Full DateTime for Japan produces Question marks

c# date time

I'm having issues trying to get the full Japanese date to display correctly in my code (I need the code to work for all regional languages around the world). In the highlighted part below is how I ...

Remove a member from an object?

php class attributes

Is there a simple way to remove a member from an object? Not just set it to null, but actually remove it.