Readkey in Java
Asked Answered
C

2

13

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

thanks

Champ answered 28/3, 2010 at 18:23 Comment(5)
I am just curious, what's the point to avoid the closing of a console application?Torbart
i am running a database query, and after it displays the result, the console window immediately closes.Champ
i am coding in a textpad and then compile and run using cmdChamp
@PetarMinchev, IDE doesn't make any difference. When you are running a java application from explorer (such as windows explorer) if application do not wait for user input, its window will close immediately.Punchboard
Title the post with your actual question.Chatterton
C
12

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.

Chenault answered 28/3, 2010 at 18:35 Comment(2)
"I am new to Java" other newbies take delight in knowing to import java.io.BufferedReader; (and import java.io.InputStreamReader;).Ticknor
I think this will read input until enter key pressed. ReadKey only wait and read for a single any character.Punchboard
H
19

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

Helmand answered 17/2, 2013 at 4:6 Comment(0)
C
12

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.

Chenault answered 28/3, 2010 at 18:35 Comment(2)
"I am new to Java" other newbies take delight in knowing to import java.io.BufferedReader; (and import java.io.InputStreamReader;).Ticknor
I think this will read input until enter key pressed. ReadKey only wait and read for a single any character.Punchboard

© 2022 - 2024 — McMap. All rights reserved.