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.

This article has been dead for over six months. Start a new discussion instead.