12

I am new to Java ... is there a similar method to the ReadKey() in C# to avoid that a console application closes?

thanks

mouthpiec
  • 3,923
  • 18
  • 54
  • 73

2 Answers2

19

YOu can also use System.in.read(), if you are ok with just 'Enter' key being your exit key.

Antony Thomas
  • 3,576
  • 2
  • 34
  • 40
11

One way to do it:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();

there are definitely shorter ones, but I think this one's convenient, because it can easily be extended.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • 9
    *"I am new to Java"* other newbies take delight in knowing to `import java.io.BufferedReader;` (and `import java.io.InputStreamReader;`). – n611x007 Oct 22 '13 at 11:19
  • 3
    I think this will read input until `enter` key pressed. ReadKey only wait and read for a single any character. – AaA Aug 16 '17 at 06:56