1

I have a java file a.java containing

int ab( fg)
abs
bcd 
abs
int x,y;

and now I want differentiate between int variable and int method and store in a different array and the output as variables: a b and methods: ab but i m confused how to differentiate both..

class ClassDemo {
    public static void main(String args[]) {
        BufferedReader br = null;
        try {
            String intarray[] = new String[50]; /*declaring array for int */
            String line, a, str = null;
            char str1 = 0;            /*declaring char str1*/
            char str2[] = new char[20];
            char str3 = 0;            /*declaring char str1*/
            char str4[] = new char[20];
            br = new BufferedReader(new FileReader("c:/java/a.java")); /*loading file*/
            while ((line = br.readLine()) != null) /*reading file*/ {
                StringTokenizer stringTokenizer = new StringTokenizer(line); /*spliting       the line into string*/
                while (stringTokenizer.hasMoreElements()) /*checking more elememts*/

                {
                    str = stringTokenizer.nextElement().toString();
                    if (str.equals("int"))/*compare for int*/ {

                        while (str1 != '(') {
                            str = stringTokenizer.nextElement().toString();
                            for (int i = 0; i < str.length(); i++) {
                                str1 = str.charAt(i);
                                str2[i] = str1;
                                System.out.println(str2[i]);
                            }
                        }
                        while (str3 != ';') {
                            str = stringTokenizer.nextElement().toString();
                            for (int i = 0; i < str.length(); i++) {
                                str3 = str.charAt(i);
                                str4[i] = str3;
                                System.out.println(str4[i]);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
2
  • please also upload the a.java file. It's a bit hard to understand your question without seeing it. Commented Sep 17, 2016 at 11:00
  • 1
    Can you please format the code. Its not even worth editing. Commented Sep 17, 2016 at 11:01

2 Answers 2

1

Reflection in java allows you to get information about the class during runtime.Reflection package has multiple class for Methods/Fields/Interface etc. An Example here.....

import java.lang.reflect.Method;
import java.lang.reflect.Field;
class DummyClass
{
    public int x;
    public int z;
  public int meathod1()
  {   return 1;
     }
  public int meathod2()
  {    return 2;
      }
  public int method3()
  {    return 3;
    }
}

class MainClass
{
   public static void main(String...s)
   {
      //Step 1: Getting the object of the class whose Fields/method u want to get.

    DummyClass obj=new DummyClass();

   //Step 2. Getting the Class of the class whose field/method u want to get.You can skip Step 1 if u already have Object the class.

     Class myClass=obj.getClass();

    //Step 3.Class has some multiple inbuilt methods to get the methods and Fields of any class.

      Method[] methodList=myClass.getDeclaredMethods();


    /*Now u have all the Methods you have declared in the class.The length of the methodList is the no of declared methods u have in your class.If u want to get the inherited methods too,then use getMethods() instead of getDeclaredMethods(),but then all your methods must be public then because getMethods() returns only public methods.*/
    System.out.println("Total No of Methods : "+methodList.length);

    //Step 4. Class has some other Inbuild methods that return Fields.

    Field[] fieldList=myClass.getDeclaredFields();
    System.out.println("Total no of Fields : "+fieldList.length);
    }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

thanks but i don't want to use reflection api i want to do it by my own method
0

In order to count int variable and int method, you need to understand possible ways that methods and variables can be declared.

both variables and methods can have accessor(private,public or protected), static, final and soon on.

the characteristics that differentiate between int variables and methods are:

1) int variables declaration will not contain open and closing brackets and will always have ; for example: int num = 0 ;

however you need to be careful: there may be a bracket after the =. for example: int num = getNum();

2) methods declaration will always contain open and closing brackets and will always have either } or ; at the end.

For example :

case 1:

int getNum(){

}

case 2:

int getNum(); //abstract or interface

however there may be complicated cases such as:

 int
 getNum(
  )
  {

      }  //<--- this is a valid function even through there is new line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.