Results 1 to 4 of 4
  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default how to enter input method name for getMethod(------)

    package getClass;

    import java.lang.reflect.Method;
    import java.util.Scanner;

    class H
    {
    void test1()
    {
    System.out.println("test1()");
    }
    void test2()
    {
    System.out.println("test2()");
    }
    }
    class Manager10
    {
    public static void main(String[] args)throws Exception
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("eneter class name");
    String className = sc.next();

    Class c1 = Class.forName(className);
    H h1 = (H)c1.newInstance();

    System.out.println("enter method name");
    String methodName = sc.next();

    Method m1 = c1.getMethod(methodName);
    m1.invoke(h1);
    }
    }

    OUTPUT

    eneter class name
    getClass.H
    enter method name
    test1()
    Exception in thread "main" java.lang.NoSuchMethodException: getClass.H.test1()()
    at java.lang.Class.getMethod(Unknown Source)
    at getClass.Manager10.main(Manager10.java:31)

    when i run the program the above output has resulted,but test1() which is in class H is not identified when i gave the input test1() for the above prog....can any one help me out how to pass the method name.???

    how to pass the method name(test1() or test2()) as input for getMethod()
    Last edited by Dayanand; 02-23-2011 at 06:00 PM.

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    first, the input for the methodname should be only test1, without the brackets !!
    second, the getMethod method is only for public members(read the API doc). You should use c1.getDeclaredMethod(methodName); !

  3. #3
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    first, the input for the methodname should be only test1, without the brackets !!
    second, the getMethod method is only for public members(read the API doc). You should use c1.getDeclaredMethod(methodName); !
    Hi.......
    Thanks for giving the valuable solution,test1 with out brackets is working fine


    Thanks & regards
    Dayanand
    Last edited by Dayanand; 02-23-2011 at 06:08 PM. Reason: i am mistaken

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    With c1.getDeclaredMethod(methodName); ? And what is your Exception now ?

Similar Threads

  1. Http Input Stream read method
    By chandan.mishra in forum Advanced Java
    Replies: 2
    Last Post: 01-03-2011, 02:36 AM
  2. input method help?
    By blazinhieu in forum New To Java
    Replies: 7
    Last Post: 03-12-2010, 03:07 PM
  3. More than one input in one private method
    By rice in forum New To Java
    Replies: 0
    Last Post: 10-02-2009, 05:08 AM
  4. help in setMethod and getMethod
    By lclclc in forum New To Java
    Replies: 5
    Last Post: 09-28-2009, 02:34 PM
  5. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 06:24 PM

Tags for this Thread

Posting Permissions

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