How to get User Input In Java [With Examples]


Taking user input in Java is the first step towards creating successful software in Java. The Java program collects input from the user through various input sources such as a mouse, keyboard, network request, CLI arguments, etc. Java also has various I/O packages that can be imported into the program to perform input-output operations. These packages support input of primitive types like double, integer, strings, etc.

This article will focus on methods like Scanner, BufferedReader, and InputStreamReader to take user input in Java.

How to Take Input in Java using the Scanner Class?

The Scanner class is one of the methods in the “java.util” package that parses and handles all the primitive type inputs. The Scanner class asks the user to enter the input, and then it prints the same on the screen or console. You can import the Scanner class from the “java.util” package in the program and create an object to use its methods.

For example,

Scanner test = new Scanner(;

In the above statement, the test is the name of an object, and is the input stream. The Scanner instance created will scan the user input in Java.

Note: It is not necessary to write as the input string in your program. You can replace it with a file and a file to read, string as the input stream.

For example,

Scanner test = new Scanner(new FileInputStream(“testFile.txt”), “UTF-8”);

Make sure that you write code {test.close()} to close the scanner when you are done editing it.

Scanner Methods for Reading Inputs from User

The following table lists the methods that the scanner uses to read the input in Java entered by the user.

MethodReturn TypeDescription
Next()StringReturns the next token from the Scanner
nextByte()ByteReads the next input as a byte.
nextFloat()FloatReads the next input as a float.
nextInt()IntegerReads the next input as an integer.
nextShort()ShortRead the next input as a short.
nextLong()LongRead the next input as a long.
nextBoolean()BooleanReads the next input as a Boolean and returns the value.
nextLine()StringAdvances the scanner one line and returns the input skipped.


The following Java program uses the Scanner class to take user input in Java (string, integer, and afloat).

import java.util.Scanner;

 class TakeUserInput


  public static void main(String args[])


  int a;

  float b;

  String c;

   Scanner test = new Scanner(;

   //Take user input as String

     System.out.println(“Type a string: “);

  c = in.nextLine();

     System.out.println(“User Input String is: “+c);

   //Take user input as Integer

     System.out.println(“Type an integer: “);

  a = in.nextInt();

     System.out.println(“User Input Integer is: “+a);

   //Take user input as float

     System.out.println(“Type a float number: “);

  b = in.nextFloat();

     System.out.println(“User Input Float number is: “+b);



Type a string:


User Input String is: Andrew

Type an integer:


User Input Integer is: 03

Type a float number:


User Input Float number is: 61.46

If you want to learn more about taking input in Java using the scanner class, check out the Multiple String Input in Java using the Scanner blog.

BufferedReader and InputStreamReader Methods

BufferedReader is another class in Java that scans the stream of characters from a character-based input stream. InputStreamReader is a function in Java that converts the input stream into a sequence of characters for BufferedReader to scan. The BufferedReader class accepts the InputStream as a parameter.

The BufferedReader class uses read() and readLine() methods to read characters and the next line and returns them.

You must import the package to create a BufferedReader class.  

// Creates a FileReader

FileReader test = new FileReader(String file);

// Creates a BufferedReader

BufferedReader a = new BufferedReader(test);

Get Software Engineering degrees online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

BufferedReader Methods for Reading Inputs from User

The following table lists the methods that the buffered reader uses to read the input in Java entered by the user.

ready()Verifies if the file reader is in a read state.
mark()Marks the position in the buffered reader up to which the data has been read.
reset()Returns the control to the point where the mark was set.


The following Java program uses a BufferedReader class alongside an InputStreamReader class to take the user input in Java.




public class test {

 public static void main(String[] args) {

  BufferedReader buffer = null;

  try {

   String scanFirstLine;

   buffer = new BufferedReader(new FileReader(“D:\\user_input.txt”));

   while ((scanFirstLine = buffer.readLine()) != null) {



  } catch (IOException a) {


  } finally {

   try {

    if (buffer != null)


   } catch (IOException b) {


   }  } } }


The following data is found in the file:

This is a sample statement that BufferedReader reads from the file.

In the above example, we created a buffered reader named buffer. The buffered reader is linked with the “user_input.txt” file.

Then, we used the readLine() method to scan the characters from the buffer of the buffered reader.


In this article, we have covered the methods used to take user input in Java. We have discussed the Scanner and BufferedReader methods, along with examples. We have also discussed the methods that Scanner and BufferedReader class uses to take input in Java. We hope that you have got a basic understanding of how to handle input in Java. If you want to learn more Java programming language, check out upGrad’s Executive PG Program in Full Stack Development course designed for working professionals.  

Plan Your Software Development Career Now.

0 replies on “How to get User Input In Java [With Examples]”

Let’s do it!
No, thanks.