Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

How to get User Input In Java [With Examples]

Updated on 17 November, 2022

6.5K+ views
5 min read

Introduction

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.

Check out our free courses to get an edge over the competition.

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(System.in);

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

Check out upGrad’s Java Bootcamp

Note: It is not necessary to write System.in 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.

Method Return Type Description
Next() String Returns the next token from the Scanner
nextByte() Byte Reads the next input as a byte.
nextFloat() Float Reads the next input as a float.
nextInt() Integer Reads the next input as an integer.
nextShort() Short Read the next input as a short.
nextLong() Long Read the next input as a long.
nextBoolean() Boolean Reads the next input as a Boolean and returns the value.
nextLine() String Advances the scanner one line and returns the input skipped.

 Example

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

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

import java.util.Scanner;
 class TakeUserInput

{

  public static void main(String args[])

  {

  int a;

  float b;

  String c;

   Scanner test = new Scanner(System.in);

   //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);

  }}

Output

Type a string:
Andrew

User Input String is: Andrew

Type an integer:

03

User Input Integer is: 03

Type a float number:

61.46

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 java.io.BufferedReader 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.

Method Description
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.

Example

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

import java.io.BufferedReader;
import java.io.FileReader;

import java.io.IOException;

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) {

    System.out.println(scanFirstLine);

   }

  } catch (IOException a) {

   a.printStackTrace();

  } finally {

   try {

    if (buffer != null)

     buffer.close();

   } catch (IOException b) {

    b.printStackTrace();

   }  } } }

Output

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.

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

Conclusion

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.  

Frequently Asked Questions (FAQs)

1. Why should you learn Java?

Java is a user-friendly language and ideal for students learning any programming language. As a language, Java helps you get down on the coding ground and find your way in the infinite software world. Additionally, Java is very versatile and flexible. Java is compatible with any operating system. Due to the pandemic, people have started using their personal devices like laptops, desktops, etc., for work. Java language is designed to work on any OS such as Mac, Windows, iOS, and Android. Moreover, in the future, Java will continue to be in demand. Mobile app downloads in 2020 have increased to 150 billion and will soon touch the 184 billion mark. Most Android applications have back-end code written in Java. The number of downloads determines the number of applications on the phone, creating more room for Java. This explains how Java will continue to dominate other programming languages in the future.

2. How much does a Java developer make?

The average Java developer salary in India is INR 4,97,611/year. As an entry-level Java developer, the numbers could vary. The salary of a Java developer also fluctuates based on their experience. The more they experience, the more they get paid. A developer is mainly focused on creating web applications that are then sold in the market. They are a part of a software development team aiming to design, maintain, write, and implement code. Having a bachelor’s in IT or computer science can help you kickstart your journey as a Java professional.

3. Which area uses Java the most?

Java has the ability to work on every platform. Furthermore, it is also free to use and an object-oriented program. Java has been taken by inspiration from the C and C++ languages. It was created to keep it easy for beginners. Some areas that widely use Java are game development, stock market algorithms, open-source development, machine learning, and artificial intelligence.

RELATED PROGRAMS