-1

I have a Menu class with a method that displays a menu, stores and then returns the selection made by the user.

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) Exit ");

        int Select = input.nextInt(); 

        return Select;   
   }
}

I want to use the return value in my main method but im not sure how. I have made an object in my main method by saying:

Menu menuObject = new Menu();

and by adding:

Menu.menuSelect();

I know that the menu runs but i want to get the return value from return Select in a variable in my main class. Any help would be much appreciated.

Maroun
  • 94,125
  • 30
  • 188
  • 241
moebeeable
  • 11
  • 3

1 Answers1

6

You simply assign it to an int:

int result = Menu.menuSelect();

Notes:

  • Follow Java Naming Conventions and name the variable menu instead (same for select)
  • If the method is static, you can call from a static manner without constructing a new object
  • If it's a helper method, and will be called only within the class itself, consider using another access modifier - private
  • Indent your code for a better world
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • And perhaps what was meant was "Name the variable 'select' instead'." Java naming conventions call for lower-case letters to start variable names. 'menuObject' is unusual, but doesn't violate naming conventions. Also, study static and instance methods; there's nothing wrong with having menuSelect be static, as you have it, but you should know how to make it an instance method also. – arcy Mar 09 '14 at 12:45
  • that makes sense. I tend to get a bit inconsistant with variable names haha. fair enough i will look into that! thank you. – moebeeable Mar 09 '14 at 12:51