Table of Contents
In this post, we will see how to take input from user in java.
There are times when you want to take input from user and run program according to user input.
There are many ways to take input from user and some of them are:
- Using Scanner
- Using BufferReader
Using  Scanner class
Scanner class is a way to take input from users. Scanner class is available in java.util package so import this package when use scanner class.
Firstly we create the object of  Scanner class. When we create object of Scanner class we need to pass System.in as a parameter which represents standard input stream and that is a predefined object. If we want to take input from a file then we have to pass object of class File.
How to create object of scanner class :
1 2 3 |
Scanner s=new Scanner(System.in); |
Here Scanner is a class, s is an object ,new is used to allocate memory and System.in is for the standard input stream.
There are various methods available in Scanner class. Few of them are:
Method | Description |
---|---|
nextInt() | It Is used to input an integer. |
nextLine() | It is used input a String. It reads input including space between words. |
nextFloat() | It is used to input a Float. |
nextByte() | It is used to input a Byte. |
next() | It is used to input string but it read the input only till space. It can not read two words separated by space. |
We can use any method according to the type of value we want to read.
Below Source code shows the use of scanner class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; import java.util.Scanner; //import util hpackage for Scanner class public class Employee { public static void main(String args[]) { int id; String name; float salary; Scanner s=new Scanner(System.in); System.out.println("Enter Employee name:"); name = s.nextLine(); //taking string input System.out.println("Enter Employee Id:"); id = s.nextInt(); //taking integer input System.out.println("Enter Employee Salary:"); salary = s.nextFloat(); //taking float input // Printing employee Details System.out.println("Employee details:"); System.out.println("Employee name is: " +name); System.out.println("Employee id is: " +id); System.out.println("Employee salary is: " +salary); s.close(); } } |
When you run above program, you will get below output.
Output:
Enter Employee name:
John
Enter Employee Id:
101
Enter Employee Salary:
20000.0
Employee details:
Employee name is: John
Employee id is: 101
Employee salary is: 20000.0
Using BufferedReader classs
By using System.in in an InputStreamReader which is wrapped in BufferedReader, we can read input from the user in console. BufferedReader is available in java.io package. when we take input using BufferedReader class it takes all the values as String so, whenever any other type of values like int, float values are required. We need to parse the value which is in string form using wrapper class for ex: Integer.parseInt for int, Float.parseFloat for float values.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; import java.io.*; //import io package for BufferedReader class public class BufferedReaderMain { public static void main(String args[]) { try //exception handling is done here using try and catch block because sometimes exception occur when uses io classes { // creation of BufferedReader object BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Student name:"); String name=br.readLine(); // to read from console System.out.println("Enter Student number:"); int num=Integer.parseInt(br.readLine()); //parsing of string into into integer type using wrapper class // Printing employee Details System.out.println("Student details:"); System.out.println("Student name is:" +name); System.out.println("Student number is:" +num); } catch(IOException e) { System.out.println(e.getMessage()); } } } |
When you run above program, you will get below output.
Output:
Enter Student number:
101
Student details:
Student name is:Martin
Student number is:101
Please note that BufferedReader class is thread safe while Scanner class is not.
That’s about taking input from user in java.