Streams in java - theoretical points
Posted on 2003-12-09
This is kind of a strange question. I don't really need a solution, as I've already found one, but I'd like to know some theory behind WHY the solution I've come with works.
A bit of background. I had an assignment for a programming class that involved graphs. My professor wrote a program to produce random graphs, that were represented as an n by n matrix, where n is the number of vertices in the graph. We were to use his program to produce a large number of graphs, and then comment of the degree of connectivity of the vertices, and draw some fairly general conclusions.
Not wanting to do more work than I had to, I tried to write a program automate running his program, to produce the random graphs. The way his program works is that there's a menu, the user chooses an option, then the program asks the user questions accordingly. So I used Runtime.exec(), and treated the input and output streams. All worked well for the choice from the menu, and for the initial questions, but at one point, his program threw a NullPointerException, because it was interpreting its input from my program as being null. I looked at his code, and realized that it was because, at the point where the NullPointerException was thrown, he was calling a new method to get input. In the new method, he created a NEW BufferedReader with System.in, instead of passing the method the current BufferedReader from the main method.
Is everyone following so far? So I modified his code, so that the main passed its BufferedReader to the second method, and all worked well.
My question is, why did that work? System.in is still System.in, no? So why, if he made a new BufferedReader with System.in, would it cease to read from the input that I'd already sent it?