I have a console menu that has to read inputs a few times in different methods. I use a new BufferedReader(new InputStreamReader(System.in))
to do that. But if this reader is closed in a method it's not useable/openable again because of System.in
.
To solve this problem one possibility is to static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
so it's usable several times in different methods with Main.br.readLine();
.
Is this a good way or are there much better methods?
Pass the BufferedReader
into your method(s) (or make it a shared field), that way you don't have to re-create it. Also, you are correct that closing System.in
(or something wrapping System.in
) will cause you issues. Instead of
public void foo()
something like
public void foo(BufferedReader reader)