Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Invoke methods

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Invoke methods

    Hi,
    Im making a simple menu style interface. Ive got a class RegistryInterface which creates the interface menu and then a class RegistryApp with a main method to initiate it.
    My problem is i have the main menu public void doMenu, within this i have options where a user enters a number 1-4 and are then taken to the chosen menu option they decide.
    Below is the basic layout of it, if a user enters 1 then i want to invoke the method doAddStudent. I know how to store the value entered by the user etc i just need to know how i would invoke the doAddStudent method from inside the doMenu method.
    Thanks for the help.
    public class RegistryInterface
    {
        private Registry aRegistry = null;
     
        public RegistryInterface(Registry aRegistry)
        {
     
        }
     
        public void doMenu(Registry theRegistry)
        {
            System.out.println("Registry Main Menu");
            System.out.println("******************");
            System.out.println("1. Add a Student");
            System.out.println("2. Delete a Student");
            System.out.println("3. Print Registry");
            System.out.println("4. Quit");
            System.out.print("\nSelect option [1, 2, 3, 4] :> ");
     
     
        }
     
        private void doAddStudent()
        {
     
        }
     
        private void doDeleteStudent()
        {
     
        }
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Invoke methods

    Quote Originally Posted by wdh View Post
    Hi,
    Im making a simple menu style interface. Ive got a class RegistryInterface which creates the interface menu and then a class RegistryApp with a main method to initiate it.
    My problem is i have the main menu public void doMenu, within this i have options where a user enters a number 1-4 and are then taken to the chosen menu option they decide.
    Below is the basic layout of it, if a user enters 1 then i want to invoke the method doAddStudent. I know how to store the value entered by the user etc i just need to know how i would invoke the doAddStudent method from inside the doMenu method.
    Thanks for the help.
    Hello wdh!
    Since doMenu() and doAddStudent() are in the same class you can simply call one from the other directly
    public void doMenu(Registry theRegistry)
        {
            System.out.println("Registry Main Menu");
            System.out.println("******************");
            System.out.println("1. Add a Student");
            System.out.println("2. Delete a Student");
            System.out.println("3. Print Registry");
            System.out.println("4. Quit");
            System.out.print("\nSelect option [1, 2, 3, 4] :> ");
     
            //if user enters 1
            doAddStudent();
     
        }
    If you have another problem, please explain it.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Invoke methods

    Ok, so you said you know how to store the answer the user gave? Next you want to do a serious of conditional statements (if/else). If the answer the user gave is 1, then call the doAddStudent() method, if the answer is 2, call the doDeleteStudent() method, ect.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Invoke methods

    Hi, i sorted the problem in the end, doAddStudent() is all i needed, i didnt realise it was that simple calling from the same class. Thanks anyway!

Similar Threads

  1. Help with methods
    By CSUTD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 10:39 PM
  2. Timer expires invoke method
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 11th, 2011, 05:52 PM
  3. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  4. how to invoke a method?????
    By amr in forum Java Theory & Questions
    Replies: 2
    Last Post: December 2nd, 2010, 11:48 PM
  5. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM