Results 1 to 16 of 16
  1. #1
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Is your methods done different things in different invokes?

    Do you know about method overloading?

  3. #3
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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)

  4. #4
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  6. #6
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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

  7. #7
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, so basically you've invoke functions properly. So why cannot you overloaded those functions?

  10. #10
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #12
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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?

  13. #13
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    thank you ! i resolve the problem by overloading the method ;)

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by binweifong View Post
    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?
    In each method you want to invoke, add additonal parameter. Set that depends on the location you calls it. And by checking it you can skip or allow to go through the normal path.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by binweifong View Post
    thank you ! i resolve the problem by overloading the method ;)
    That's what I told you at my very first replay. Those are concepts have to use in programming, know as OO concepts.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. call a JSP method
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 09-26-2011, 04:19 PM
  2. Trying to call a method from sub class
    By TheNewGuy in forum New To Java
    Replies: 4
    Last Post: 10-17-2010, 07:08 AM
  3. I can't get to call a method to another method..
    By arnelcolar in forum New To Java
    Replies: 2
    Last Post: 10-21-2009, 12:21 PM
  4. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •