top of page
shitalpatel17

How to take input in java from user


There are two ways that we can take input in Java from the user or from a file.

  • Buffered Reader Class

  • Scanner Class

1)Buffered Reader Class: -

  • Buffer reader class is a subclass of reader class which is used to read a sequence of characters.

  • It provides a redline () method to read an array of characters, strings, and text lines.

Buffered Reader class provides two constructors:

  • Buffered Reader (Reader rd): It uses a Reader to read data from the character input stream and creates a default sized input buffer.

  • Buffered Reader (Reader rd, int size): it is used to create a buffered character input stream that uses the specified size for an input buffer.

Java Buffered reader class method

  • Int read (): - it is used for reading a single character.

  • Int read(char[] cbuf, int off, int len) (): - it is used for reading characters into a portion of an array.

  • boolean markSupported (): -it is used to test the input stream support for the mark and reset method.

  • String redline(): - it is used to reading a line of text.

  • Boolean ready (): - it is used to test whether the input stream is ready to be read.

  • Long skip (long n): - it is used for skipping the characters.

  • Void reset (): - it repositions the stream at a position the mark method was last called on this input stream.

  • Void mark (int readAheadLimit): - it is used for making the present position in a stream.

  • Void close(): - it closes the input stream and releases any of the system resources associated with the stream.

  • The redline() accepts a string from the keyboard and returns a string.

Example of a Java program to take a string as an input from the user or keyboard using Buffered Reader class.

How to Take Input in Java using Scanner class

  • We can use scanner class of java.util package to take an input from user, keyboard, or a text file.

  • When a scanner class takes an input ,it breaks the input into many pieces called tokens.

  • We can retrieve these tokens from the Scanner object with different methods like: -

next() – to read a string.

nextByte() – to read a byte value.

nextInt() – to read an integer value.

nextFloat() – to read a float value.

nextLong() – to read a long value.

nextDouble() – to read a double value.


We are taking input from user through below statement.


Scanner sc = new Scanner(System.in);


*System.in represent input stream object which represent a standard input device, ex. Keyboard by default.


Example of a Java program to take different types of input from the user or keyboard using Scanner class.

The choice of using a Buffered Reader or a Scanner depends on the code you are writing, if you are writing a simple log reader Buffered reader is adequate. However, if you are writing an XML parser Scanner is the more natural choice.

6 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Opmerkingen


bottom of page