public class Input {

    static InputStream reader = null;
    /**
     * @param args
     * @throws IOException
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws IOException, InterruptedException {

        Test test = new Test();
        test.start();
        String str = "1 21";
    //  Scanner scan = new Scanner(System.in);
        try {
            reader = new ByteArrayInputStream(str.getBytes("UTF-8"));
            System.setIn(reader);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

}

class Test extends Thread {

    public void run() {
        super.run();
        try {
            getSum();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void getSum() throws InterruptedException {

        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();

        int y = scan.nextInt();
        System.out.println(x + y);

        scan.close();
        this.interrupt();
    }

}

When I am uncommeting the line, then it is throwing exception. Main thread Exception. Can you please explain why it is so? Thanks in advance.

738 posts and you still don't post the complete text of the exception message and stack trace? Come on now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.