I like to read from a console in Java. What is the most convenient way to do it for the sake of the processing of the input? For example I want to read a single char or an integer.
Asked
Active
Viewed 69 times
-4
-
3Possible duplicate of [How can I read input from the console using the Scanner class in Java?](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – Am_I_Helpful Apr 19 '18 at 18:05
-
click the check mark by the answer that fixed your problem please – MMelvin0581 Apr 19 '18 at 18:09
-
Yeah, it could point to the right thing, but if I don't know if Scanner or BufferedReader, or something else is better to ask what. – Marjan100 Apr 19 '18 at 18:17
-
what do you mean with better? – Lodewijk Bogaards Apr 19 '18 at 18:37
-
I mean I have encountered the above mentioned ways, but I don't know what is more suitable in a specific situation. – Marjan100 Apr 19 '18 at 18:39
2 Answers
1
import java.util.Scanner;
Scanner input = new Scanner(System.in);
int toBeRead = Integer.parseInt(input.nextLine());

MMelvin0581
- 509
- 6
- 20
-
Be aware that `replace()` and `replaceAll()` are both slow because many many string objects are created inside the method call: https://stackoverflow.com/questions/6262397/string-replaceall-is-considerably-slower-than-doing-the-job-yourself – T Tse Apr 19 '18 at 19:10