+ 2

What is equivalent of getch() in java?

31st Jul 2017, 6:49 PM
Prathamesh Sawant
Prathamesh Sawant - avatar
7 Réponses
+ 8
Use an extra nextLine as the following example : Scanner scan = new Scanner (System.in); int a = scan.nextInt(); scan.nextLine(); // serves as getch() String str = scan.nextLine();
31st Jul 2017, 7:29 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
@Thomas Thank you, for the link and explanation!
31st Jul 2017, 9:50 PM
Manual
Manual - avatar
+ 3
API says it Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. https://docs.oracle.com/javase/7/docs/api/
31st Jul 2017, 7:07 PM
Thomas Zenglein
Thomas Zenglein - avatar
+ 2
getch() in c wait for user to press key. Will System.in.read() do the same job?
31st Jul 2017, 7:04 PM
Prathamesh Sawant
Prathamesh Sawant - avatar
+ 1
Try System.in.read() this returns a int value. I don't know what getch() returns, but eather way convertion should not be the problem.
31st Jul 2017, 7:01 PM
Thomas Zenglein
Thomas Zenglein - avatar
+ 1
bookmarked 💾
31st Jul 2017, 7:39 PM
Manual
Manual - avatar
+ 1
@Manual your welcome
31st Jul 2017, 7:41 PM
Thomas Zenglein
Thomas Zenglein - avatar