In this tutorial, I am going to show you how to take input from user in java using two different ways. You can follow any method according to your convenience. To understand how we get input from user in java we first must know of the following two things :

System.out
– Java uses System.out to refer to standard output device.
System.in – Java uses System.in to refer to standard input device.

By default output device is monitor and input device is keyboard. To perform console output, we simply use println method to display a primitive value or a string to the console. Console input is not directly supported in Java, but we can use the Scanner class to create an object to read input from System.in or we can use BufferedReader from java.io package.

How To Take Input From User In Java Using Scanner Class

Taking Input Using Scanner Class : Scanner input = new Scanner( System.in )

The above line : Scanner input = new Scanner( System.in ) creates a scanner object and assigns its reference to the variable input.

Note : Don’t worry if you find it hard to understand what these things mean and what they are doing. Once we come to the topic classes and objects then you’ll understand everything.

Various methods which can be invoked to take various types of input.

     Method                Description    
   nextByte( )  reads an integer of type byte
   nextShort( )  reads an integer of type short
   nextInt( )  reads an integer of type int
   nextLong( )  reads an integer of type long
   nextFloat( )  reads a number of type float
   nextDouble( )   reads a number of double type
   next( )  reads a string that ends with a white space character
   nextLine( )  reads a line of text ( ends when Enter key is pressed )

Now lets do a simple program that takes input of type int, double and string. All other input can be taken in the same manner.


  // How to take input from user in java using Scanner class
  // How to get input from user in java using Scanner class
  // Taking input of type int, double and string
  import java.util.Scanner;
  public class TakeInput
  {
         public static void main(String[] args)
  { 
     int number;
     double decNumber;
     String text;
 
     Scanner input = new Scanner(System.in);
     System.out.print("Enter an integer number : ");
     number = input.nextInt();
     System.out.print("Enter a decimal number : ");
     decNumber = input.nextDouble();
     System.out.print("Enter a line of text : ");
     text = input.nextLine();
 
     System.out.println("Ingeger Number : " + number);
     System.out.println("Decimal Number : " + decNumber);
     System.out.println("Text : " + text);
         }
  }

Now in the very first line I have imported Scanner class which is in java.util package. ( import java.util.Scanner )

How To Take Input From User In Java Using BufferedReader Class

Now let me show you another method to take input from the user.

BufferedReader input = new
                  BufferedReader( new InputStreamReader ( System.in ))


  // How to take input from user in java using BufferedReader class
  // How to get input from user in java using BufferedReader class
  // Taking input of type int, double and string
  import java.io.*;
  public class TakeInput
  {
         public static void main(String[] args)throws IOException
  {   
     int number;
     double decNumber;
     String text;
 
     BufferedReader input=new
      BufferedReader(new InputStreamReader(System.in));
 
     System.out.print("Enter an integer number : ");
     number = Integer.parseInt(input.readLine());
     System.out.print("Enter a decimal number : ");
     decNumber = Double.parseDouble(input.readLine());
     System.out.print("Enter a line of text : ");
     text = input.readLine();
     
     System.out.println("Ingeger Number : " + number);
     System.out.println("Decimal Number : " + decNumber);
     System.out.println("Text : " + text);
         }
  }


The code import java.io.*; imports all classes from java.io package. In the above programs I have used System.out.print( ) and System.out.println( ).

System.out.println( )
– prints the text in it and moves the cursor to the next line.
System.out.print( ) – prints the text in it but do not move the cursor to the next line.

Note : Java is a case sensitive language so be careful while coding.

Leave a comment

Your email address will not be published. Required fields are marked *