Basic Input

Contents

keyboard_tab
Before Starting Know-How
1
Introduction
PREVIEW19s
2
Basic Output
PREVIEW7m 42s

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
29m
Students
41
Ratings
5/5
starstarstarstarstar
Description

In this course, we touch on key topics that should be known before learning the basics of Java. 

Learning Objectives

  • Java Output
  • Java Input
  • Java Comments

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge
Transcript

Hi there. In the previous video, we talked about output in Java. In this video, we will talk about the Basic Input type in Java. There are several ways to get input from the user in Java. In this section, we will learn to get Input by using the Scanner Class. In order to use the Scanner Class, firstly, we need to import the Scanner class by using import java.util.Scanner. Don't worry if you don't understand it. We'll learn more about classes and how to import them in the next lectures. 

Then, we'll create an object of the scanner class. Then, the next N-I-T method of the scanner class is used to get integer input from the user, and we assign this input to a variable. And finally, we display the number on the screen by the println method. If you're ready, let's move on to the Eclipse and make some examples by using the Scanner Class. In exercise project, right-click on the Inputoutput package and select "New," "Class." Specify the class name as BasicInput and select the check box for the main method. 

And lastly, I click the 'Finish' button. Let's look at Java input. We will use the Scanner Class for getting input from the user in Java. Firstly, we need to import the Scanner Class. If we talk briefly about the import process, there are many packages that are prepared in Java. There are various classes in each of these packages as well. In order to use these classes in our own project, we need to declare this here with the import Keyword. This is the meaning of the import process. Now, let's create an object from the Scanner Class. I'm typing Scanner with the first letter capitalized. If you notice, when you type 'Scanner', the editor offers us Scanner Classes in different packages. 

If we select the Scanner Class of the java.util classes from here and press 'Enter', this class will be imported automatically. But first, I'll call the Scanner Class without importing it. I want to show you the error that will occur if we do not import it. Let's continue now. The name of the object that I'll create from the Scanner Class can be the inputNumber. InputNumber= New scanner(). I write InputNumber= New scanner(system.in) in the parentheses. To create an object of the Scanner Class, we usually pass the predefined object system.in, which represents the standard input stream. We may pass an object of class file if we want to read input from a file. 

If you place the mouse pointer on the scanner word, import options are shown. We choose the Import Scanner Class in java.util package to import the scanner class. And notice, when we import the scanner class, the phrase 'import java.util.Scanner' is added just below the package name. If we replace Scanner with asterisk here, it means that we automatically import all classes from the java.util package. Let's continue now. We show a message to the user to enter a number by using the println method. This message can be 'Please enter a number.' And we declare a variable number with integer type, int number =. 

And to sign the next int method of Scanner Class to this number. So, after the equal sign, I write =inputNumber.next. If you notice there are many different scanner methods such as Next, Next Line, Next Float, and Next Double. According to the type of data you want to receive from the user, you should choose the appropriate method from here. For example, we now ask the user to enter an integer value. Therefore, we will use the nextInt method. And we show the value of the number variable by using the println method. Let's run the code. On the Console screen I enter '5' and it shows 'you entered 5.' There is no need to assign the nextInt method to a new variable. We can use directly the inputNumber.nextInt method in the println method. Let's run the code. 

I will enter a number and press 'Enter', and as you can see, the result is the same. Also, we can get String input from the user. The String is a series of characters. Let's change the code. For String input, we use the next method of scanner. We declare a variable text and assign the next method of Scanner Class to this text. And this time the message can be 'Please enter a text'. Let's run the code again. In the console screen, we enter java, and it shows 'you entered java.' There is no need to assign the next method to a new variable. We can use directly the InputText.next method in the println method. Let's run the code again. I type a text and press 'Enter.' 

As you can see, the result is the same. Now I want to show you something else. Let's run the application once again. This time I'm typing Java programming on the console screen. Note that when you press 'Enter,' only the word Java is printed on the console screen, not the word programming, because the next method only takes the first argument, ignoring any arguments after a space. If we're asking the user to enter a value consisting of more than one word, this time we should use the Next Line Method instead of Next. Now let's change this to Next Line. Now let's run the application once again. I'm typing java programming on the console screen and press 'Enter.' And as you can see, this time we were able to print both the words Java and Programming to the console. Yes, friends, this is the basic output operation in Java. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
458
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics