Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

horrible at java programming, need help "invoking methods?"?

assume i'm an idiot b/c with programming i am.

okay so for my final i need to create a database that gives multiple options, choices, etc.

what i have problems with (besides everything) is getting information passed from one method to another and keeping the program going, if there are two methods it always stops at the end of the first one b/c i have no idea how to send the info (i.e. user input of the number '5') to the next method.

TO THE POINT: i was just hoping if someone could show me a very simple small example (it can be about apples and oranges/dogs and cats) of passing user input from one method to the next. i'm assuming passing the info will keep the program running.

my assign. says that each of my options (i.e. add, delete, edit, bla) will invoke a method.

P.S. i am using global static variables if that matters at all.

i'm also aware this may make no sense to anyone but me, but googling this is getting nowhere.

BQ: if i do get a program with multiple methods to run its complete course, how do i make it return to a certain method w/o undoing the info entered.

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    a very simplified example of a method call would be (this is inside a class, of course)

    public static void foo(){ //your first method

    //...stuff

    doSomething(variable); //this calls the method doSomething()

    //...more stuff //when the second method is done, it will return here

    }

    public static void doSomething(int x){ //the second method, gets called from the first one

    //...more stuff //it also has a variable that was passed to it in the

    } //other method

    i hope that makes some sense. pretty much you are doing stuff in the first method, then you call the second method and give it a variable, which it can then use, then it will return to where it left in the previous method. it wont change any of the numbers in the first method, as long as you dont change any of the global static variables.

    Sorry, the white space got deleted by yahoo, it looked much better when I typed it. If you need more help, feel free to email me or something, gmail doesn't delete whitespace like that - stphndemos@gmail.com

  • 9 years ago

    Like any other programming task, you start by breaking it down into pieces. Passing user input from one method to another consists of first acquiring the user input, and then passing it to a method. So, do those things separately.

    You've probably already learned how to use a Scanner pointing at System.in to get text, numbers, etc from the console. So first you do that. Then when you have the data, you pass it as arguments to a method that takes arguments of the appropriate type. You might have to perform a conversion (for instance, from a string to a numeric value) in order to match the types of arguments the method takes.

    Nothing is 'undone' when a method returns. You lose the local data in that method, but anything that was passed out is still available for use elsewhere.

  • 7 years ago

    tricky stuff. research from search engines like google. this might help!

Still have questions? Get your answers by asking now.