COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business Administration | STEMMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration Master of Business Administration-90 ECTSMSc International Business ManagementMS Data Science Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMaster in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc in Sustainable Global Supply Chain ManagementMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business AdministrationMaster of Business AdministrationMSc in International FinanceMSc in International Management and Global LeadershipMaster of Business AdministrationBachelor of BusinessMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 90 ECTSBachelor of Business AnalyticsBachelor of Information TechnologyMaster of Business AdministrationMBA Business AnalyticsMSc in Marketing Analytics and Data IntelligenceMS Biotechnology Management and EntrepreneurshipMSc in Luxury and Fashion ManagementMaster of Business Administration (90 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS) MSc in International Corporate Finance MSc in Sustainable Luxury and Creative IndustriesMSc Digital MarketingMSc Global Supply Chain Management (PGMP)MSc Marketing (PGMP)MSc Corporate Finance (PGMP)MSc Data Analytics for Business (PGMP)MS Business AnalyticsMaster of Business AdministrationMS Quantitative FinanceMS Fintech Management
For College Students

What is user input in Java?

$$/$$

Let’s now move to the next video where you will learn how your code can ask a user to input values of his/her choice.

You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with Intellij. Please be careful with the name of the java file. The name of the java file should exactly be same as the class name inside it for the code to run. So make changes accordingly.

 

$$/$$

Video Transcript

 

Let us now see how we can take a user input. So basically for taking a simple user input, first you have to write a statement import Java util scanner. For now, you don't need to worry about what this statement is doing, but rather just accept that you have to write this statement in order to be able to take a user input. So now we say we declare a public class user input and inside this we write the public static void main statement. Then I declare two simple variables int number A and int number B. Then I say scanner input is equal to new scanner system in. So basically what this statement does is that it creates a new variable object called input which belongs to the scanner class. You don't need to worry about what the scanner class is, but just assume that creating this object is necessary to be able to take the user input. And it is only this object which will facilitate us taking the input from the user. So I say scanner input is equal to new scanner system in. This is a standard way of declaring a new object through which we can take the user input. Then we print on the console, enter the value for A. Then I say number A is equal to input next int. So basically next int is an inbuilt function of this particular scanner object. You don't need to worry about it, but just assume that this function helps to take in the user input. So this function takes in the user input and puts it inside the variable number A. Similarly, I then print on the console, enter the value for B and I do a similar operation. I call the next int function on my input object and whatever the user inputs I store it inside number B. And then finally, after taking input number A and number B from the user, I print their addition here. So let us try to run this file and see how it works. So when I run, as you can see, first enter the value for a statement is printed. So now the user is supposed to input a value for A. Suppose I am inputting four, then I press enter. Then I get the second print statement which says enter the value for B. Now you must enter the value for b. Suppose I enter five here and press the enter key. So now you can see that nine is getting printed on the console. So this nine here actually comes from the addition of number A and number B. So basically what happens is that once you enter four here on the console, the next int function is getting called and this four is getting stored inside number A. Similarly, when you enter five here on the console, again this five is getting stored inside the number B variable by calling the next int function on input. Finally we print the addition of number A and number B. So in this way, we have successfully been able to take input from the user and print the addition of any two numbers which are input by the user.

 

Video Recap

 

  • To take a user input in Java, you need to write the statement "import Java util scanner".

  • Declare a public class "user input" and write the public static void main statement.

  • Declare two variables: int number A and int number B.

  • Create a new variable object called "input" belonging to the scanner class by writing "scanner input is equal to new scanner system in".

  • Print on the console "enter the value for A" and use the "next int" function to take the user input and store it inside the variable number A.

  • Similarly, print on the console "enter the value for B" and use the "next int" function to take the user input and store it inside the variable number B.

  • Finally, print the addition of number A and number B.

  • To run the file, input the value for A, press enter, input the value for B, press enter, and the sum of A and B will be printed on the console.

  • This is a simple way to take user input and perform an operation in Java.

 

That was interesting! You used the Scanner class to prompt the user to input the values.

 

Explanation of the Scanner Code

First you wrote the following statement to be able to use the Scanner class:

Scanner input= new Scanner(System.in);

Here, “input” is the name of your Scanner. You can name the Scanner absolutely anything(like choosing variable names).

This statement “new Scanner(System.in)” prompts Java to provide you with a new Scanner to “scan” the input given by users.

 

You can then use the following statement to read some integer data given as input from the user:

a=input.nextInt();

Here, the “.nextInt()” keywords tell you scanner(named “input”) to look for the next data entered by the user and store it in the variable “a”.

 

Remember, for Java to know where should it get the code for the Scanner from, you have to add the following statement at the top of your code file:

import java.util.Scanner;

This statement merely tells Java to “import” all the code required to use the Scanner to take user input.

Note: Often, your IDE will automatically add this import statement when you try to use the Scanner in your code.

 

Being able to take user input will be of great use in the future too, so try to remember it well.