Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Suppose i have a class named Invoked, to which i want to call using reflection from Invoker class. but i want to give input from array declared in Invoker class whenever it get code for input from keyoboard.

class Invoked {
    public static void main(String ar[]) {
        java.util.Scanner s = new java.util.Scanner();
        // here i want to give input stored in array except of console from Invoker class.
        System.out.println("input given from keyboard is : " + s.next());
    }
}

class Invoker {
    public static void main(String ar[]) {
        // i want to pass array here for keyboard input values like
        Class<?> cl = loader.loadClass("Invoked");
        Method m = cl.getDeclaredMethod("main", new Class[] {String[].class });
        m.setAccessible(true);
        m.invoke(null, new Object[] {null });

    }

}
share|improve this question
    
what do you mean by give input from array? –  jbutler483 1 hour ago
    
i want to make an online java compiler, so Invoked class will be given from user. i want to provide facility for keyboard input which user will give in a textbox before execution and will be stored in an array. now whenever it gets code for keyboard input, it provide input for that from array programatically. Please tell me alternate solution also if u have. Thanks. –  ashish kumar gupta 1 hour ago
    
I think you don't need scanner,if you are passing values from user input as array to method. Why don't you directly use array elements? –  TAsk 1 hour ago
    
the code of Scanner will be written by user, who use my online java compiler. i can not change that code. i just want to take input from user before execution. so that i will push this value for input whenever keyboard input will be asked at execution time. –  ashish kumar gupta 1 hour ago

2 Answers 2

java.util.Scanner() reads by default from System.in [stop: there is no default constructor in Scanner, your code won't compile]. You can set it to eg a StringInputStream right before invoking of main (System.setIn()).

class Invoked{
public static void main(String ar[])
{
java.util.Scanner s=new java.util.Scanner(System.in);
//here i want to give input stored in array except of console from Invoker class.
System.out.println("input given from keyboard is : "+s.next());
}
}

class Invoker
{
public static void main(String ar[])
{
//i want to pass array here for keyboard input values like 
Class<?> cl = loader.loadClass("Invoked");
Method m=cl.getDeclaredMethod("main", new Class[] { String[].class });
m.setAccessible(true);
System.setIn(new StringBufferInputStream("a b 100"));
m.invoke(null, new Object[] { null });

}

}
share|improve this answer
    
In above code i forgot to write System.in, that line was actually Scanner s=new Scanner(System.in); we can not change in this line because this code will be given from user randomly. actually i want to create online java compiler. please read all the requirement in comment, which i wrote below question. –  ashish kumar gupta 1 hour ago
    
thank you. your code is working. but please give vote for my question. –  ashish kumar gupta 10 mins ago

Use an ArrayList to make a dynamic array based off how many lines the user inputs.

class Invoked {
    public static void main(String[] args) {
        java.util.Scanner s = new java.util.Scanner(System.in);
        List<String> input = new ArrayList<>;
        while(s.hasNext()) {
             input.add(s.next());
        for(String i : input)
             System.out.println("input given from keyboard is : " + i);
    }
}

To convert the ArrayList to an array, use

String inArray = input.toArray();
share|improve this answer
    
i can not take input from keyboard at runtime. it is my condition. i want to keep value in an array. whenever we get any code at runtime like s.next(). i will fulfill this input from array except of console. please hava a look again. thanks. –  ashish kumar gupta 58 mins ago
    
and i can not write our logic in Invoked class. pls write logic in Invoker class whatever required. –  ashish kumar gupta 57 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.