简体   繁体   中英

How to Create class declare Object and then assign value

The user enters an expression. Suppose user entered the following as input:

new y java.util.ArrayList int:5

i have successfully tokenized the string and stored it into different locations of my String array. next thing i want to do is that i should check whats on the index and do things according as mentioned in the above input for reflection. Am stuck how to do it. here is my code so far

 public static void handling_input()
    {
         System.out.println("Welcome To Java Command Prompt: ");
         aLine = null;
         try
         {
            System.out.println("Enter The Command Line Expression: ") ;
            keyboard = new BufferedReader(new InputStreamReader(System.in));
            aLine = keyboard.readLine();
         st =  new StringTokenizer(aLine);
         dt = new StringTokenizer(aLine);
         }
         catch (IOException ex)
         {
             System.out.println("Error reading input!");
         }

    }
    public static void storing_tokens()
    {
        int counter =0;
        while(st.hasMoreTokens())
        {
            counter++;
            st.nextToken();
        }
        int i=0;
        expression_keeper= new String[counter];
        do
        {
            expression_keeper[i] = dt.nextToken().toString();
            i++;
        }while(dt.hasMoreTokens());
    }
    public static void token_classification()
    {
        for(int i=0; i<expression_keeper.length; i++)
        {
            if(expression_keeper[0].equalsIgnoreCase("new"))
            {

            }
            else
            if(expression_keeper[0].equalsIgnoreCase("call"))
            {

            }
            else
            if(expression_keeper[0].equalsIgnoreCase("print"))
            {

            }
            else
            {
                System.out.println("Invalid Script!");
            }
        }
    }
}

Inside this if condition:

if(expression_keeper[0].equalsIgnoreCase("new"))
        {

        }

i want to create the specified class,its object and assign values to the modifiers mentioned!

It is unclear to me what your input string tokens really mean. Is "java.util.ArrayList" the type for "y" and should it have an initial size of 5 units? Or should the first element be an integer of 5?

In the past I have found writing my own syntax tokenizer and parser a complicated thing to do. Even in simple cases I have often found that using something like JavaCC was easier in the long run.

By specifying your syntax formally you give much but structure to your code and it's debuggability. And then as said elsewhere use introspection to do the creation. The packages to do this are in java.lang.reflect .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM