Results 1 to 16 of 16
- 12-06-2010, 04:39 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
how to call one method from another method
hi im new here :)
im having difficulties in calling a method from another method
ive read a few threads but nothing make sense for me
see,
at first i call a method, i name it methodA because there are tons of methods in my assignment, then in methodA i invoke method B, the arguments passed are in the class of methodA. So then i invoke methodC in methodB, which the arguments passed are in the class of methodB.
Now i have three methods, the problem is, in methodC, i prompt for user selection, either to perform operation in methodB or back to the main menu which is methodA, how could i possibly pass them back to methodA or B without arguments (because arguments are not in that method, i do not have a argument to pass back to A or B)
i hope you guys can understand the problem i have. And can someone explain how and why? greatly appreciate
- 12-06-2010, 05:06 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Is your methods done different things in different invokes?
Do you know about method overloading?
- 12-07-2010, 06:51 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
thanks for replying :D yes i know what is overloading, its kind of using the same method name, but different signature i guess right? :) but the problem is i dont even have parameter to pass on, okay maybe i should eleborate more
here is what i actually did
the first method is a main menu, a few selections is given to be chosen by user
then the second method is about displaying names of student to check their records
then the third one is about asking user to choose whether to go back to the first method or the second, which means its their choice to return to the main menu or perform the same task again(second method)
- 12-07-2010, 06:53 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
in the first method, i passed in arraylist of menus,
in the second method, i passed in arrays of names,
in the third i passed in this processed names and there is no arguments for me to pass back to the first or second method.
- 12-07-2010, 06:57 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Still I'm not clear what you exactly want to do. Can you show your code over here to see. Or use a simple pseudo code to explain what you really want to do.
- 12-07-2010, 08:39 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
Java Code:import java.util.*; import java.io.*; public class TestStudent { public static void main (String [] args) throws Exception { int choice=0; ArrayList studlist = new ArrayList(); studlist = retrieveRecord(studlist); choice = mainMenu(choice); FunctionList(choice, studlist); //taskPerformed(choice); } public static ArrayList retrieveRecord(ArrayList a) { String name; String address; String contact; String country; try { File inFile = new File ("StudList.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inFile))); //Reads the first line (name) of the list, if name is not a null value then continue reading the second, third and forth line name = reader.readLine(); while(name != null) { Students stud = new Students(); stud.setName(name); contact = reader.readLine(); stud.setContact(contact); address = reader.readLine(); stud.setAddress(address); country = reader.readLine(); stud.setCountry(country); a.add(stud); name = reader.readLine(); } reader.close(); } //Catches any error conditions catch (IOException e) { System.err.println("Unable to read from file"); System.exit(-1); } return a; } public static int mainMenu(int selection) throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("Main Menu: "); System.out.println("1. List student's detailed records "); System.out.println("2. Ammend student's record "); System.out.println("3. List student's name from a given country in alphabetical order"); System.out.println("4. Remove student record"); System.out.println("5. Add student record"); System.out.println(); System.out.print("Enter your choice: " ); selection = scanner.nextInt(); return selection; } public static void FunctionList(int n, ArrayList a) { switch (n) { case 1 : ListStudent(a);[COLOR="Red"]//i still haven finish my coding, this is only selection 1, which is the first choice in main menu[/COLOR] } } [COLOR="Red"]i passed the above method here[/COLOR] public static void ListStudent(ArrayList n) { Object [] arr = n.toArray(); //passing an arraylist Scanner scanner = new Scanner(System.in); int studID; System.out.println(); for(int i = 0; i < arr.length ; i++) { Students stud = (Students)arr[i]; //casting System.out.print( i+ " " +stud.getName()); System.out.println(); } try { System.out.println(); System.out.print("Selection: "); studID = scanner.nextInt(); showStudentRecord(studID, arr); [COLOR="Red"]pass again to method below[/COLOR] } catch (InputMismatchException e) { System.out.println("Input error!"); System.exit(-1); } } public static void showStudentRecord(int b, Object [] c) { Scanner scanner = new Scanner(System.in); char input; System.out.println(); switch (b) { case 0: System.out.println(c[0]); break; case 1: System.out.println(c[1]); break; case 2: System.out.println(c[2]); break; case 3: System.out.println(c[3]); break; case 4: System.out.println(c[4]); break; default: System.out.println(" Input Error!"); } System.out.println(); System.out.println("Do you want to continue? (Y/N): "); input =(char)System.in.read(); switch(input) { case 'Y': ListStudent(); break; case'N': mainMenu(); break; default: System.out.println("Invalid Input!"); [COLOR="Red"]i do not have arguments to pass back to ListStudent() and mainMenu()[/COLOR] } } }Last edited by Eranga; 12-07-2010 at 10:43 AM. Reason: code tags added
- 12-07-2010, 08:41 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
i also use get and set method to get and set value into variables, which i saved them in a class , tell me if you need it;)
- 12-07-2010, 10:46 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags next time when you are posting code segments. If you don't know how to do that, check on my forum signature.
- 12-07-2010, 10:55 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Okay, so basically you've invoke functions properly. So why cannot you overloaded those functions?
- 12-07-2010, 12:09 PM #10
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
awh im sorry cause im new here, ill go check later (:
you see the last few lines there in my code, ive tried to invoke listStudent() and mainMenu() (trying to get user back to the previous method), but i dont know how to pass them back without putting arguments in them. e.g listStudent(arraylist), if i put something in it, the program gives me error
- 12-08-2010, 12:27 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can do one tricky thing here, quite a bad design for me anyway. Pass another argument with it, a status to identify from where it invokes. Depends on the status you can skip the process or continue.
- 12-08-2010, 04:19 AM #12
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
err, sorry but im not clear with that, can you tell me how? like how do i make user focus back to mainMenu using method? what arguments should i declare and pass?
- 12-08-2010, 05:56 AM #13
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
thank you ! i resolve the problem by overloading the method ;)
- 12-08-2010, 11:08 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-08-2010, 11:11 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-08-2010, 11:24 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And could you please mark the thread solved.
Similar Threads
-
call a JSP method
By sauravsinha in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 09-26-2011, 04:19 PM -
Trying to call a method from sub class
By TheNewGuy in forum New To JavaReplies: 4Last Post: 10-17-2010, 07:08 AM -
I can't get to call a method to another method..
By arnelcolar in forum New To JavaReplies: 2Last Post: 10-21-2009, 12:21 PM -
how to call method?
By leapinlizard in forum New To JavaReplies: 9Last Post: 04-29-2009, 11:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks