0
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.

2
Contributors
1
Reply
10
Views
1 Year
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.