I have to read integers more frequently in Java. So how i should do? What function i have to use?
C/C++ too have buffering concepts but they has a single statement. But in Java we have to call at least 2 function to read an integer.
For an example,
To read an integer from console,
I will use
scanfin Ccinin C++
But in Java,
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(System.in)); String number = bufferedreader.readLine();
int value = Integer.parseInt(number);
or
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
Someone please explain me: Can I have a simpler version than these similar to cin or scanf (A one line statement).
Thanks in advance.
I believe
Scannerprovides the simplest interface. I don’t believe there is a simpler approach provided by the standard API. (You could of course encapsulate the snippet in a method, and call the method instead.)The only one-liner I can come up with is the obvious