In the last post, we learned how we write a string to console using System.out.println() method. Sometimes, we need input from the user and then process the details (calculations, string manipulation, storing and retrieving data etc.) and then the program generates the output data as required. Here is an example which gets an integer input from the user and prints the same number -
Class GetInput (GetInput.java)
import java.util.Scanner;
public class GetInput {
public static void main(String args[]) {
// Creates an object of Scanner class by calling Constructor method and
// passed System.in (InputStream class object) as an argument
Scanner sc = new Scanner(System.in);
// Asks to user to enter an integer number
System.out.print("Enter the integer number: ");
// Prompts for user to enter the number and wait till user press enter button
// The number is assigned to variable i
var i = sc.nextInt(); // Or int i = sc.nextInt();
// Prints the number user entered
System.out.println("You entered number " + i);
}
}
In the above example, we use the import statement as the first statement. When we want to reuse any classes, fields or methods from Java API or other sources, we need to have import statement as first statement in the program to include the whole package or specific class. Till now, we have used only System class, which is defined in the java.lang package. We don't need to import java.lang package in any java code as it is included by default. We can use all java.lang package classes, methods, interfaces and fields without importing the package. In the above program, we need to import class Scanner from package java.util to get the user input from the console -
import java.util.Scanner;
Scanner Class
Scanner class has a final class imported from the package java.util, to get the input from the different sources. Final class means you can not inherit (subclass) from the Scanner class. Scanner is used to get inputs from various sources, like the input stream, file, buffered reader and breaks input into tokens by using the delimiter. Scanner uses white space (white space characters include blanks, tabs and line terminators) as a default delimiter, but there are methods to set the different delimiter. (We will discuss later more about delimiters.) Following statement creates an object of Scanner -
Scanner sc = new Scanner(System.in);
In Java, mostly an object is created by calling the constructor method from the class. Constructor method name is the same as the class name - in this case, Scanner. We passed System.in (a field in class System of type InputStream) as an argument to the constructor, which represents console / terminal.
System.out.print("Enter the integer number: ");
Here, the program asks the user to enter an integer number by calling the Scanner's method nextInt(), which prompts the user to enter an integer number and assigns the number to variable i. We can use var or int keywords to declare the integer variables.var i = sc.nextInt(); // Or int i = sc.nextInt();
System.out.println("You entered number " + i);
Finally, we print the same entered number to the console using the method System.out.println(). Here we use a fixed text "You entered number" and then use the value of the variable i. We use the + sign here to make it one string. This is called string concatenation. Here is the output -
NOTE: We will talk later more about constructors, packages, inheritance, I/O Streams in detail. We will also learn more about Scanner class by using more advanced methods to get specific input and how to make sure to validate the input data.
Enter the integer number: 8
You entered number 8
NOTE: We will talk later more about constructors, packages, inheritance, I/O Streams in detail. We will also learn more about Scanner class by using more advanced methods to get specific input and how to make sure to validate the input data.
Code can be found here at Github link.
Exercise: Write a program to get input of type Double and print the same.
Exercise: Write a program to get input of type Double and print the same.
No comments:
Post a Comment