Taking user input and displaying the correct output are two extremely important aspects of any programming language. A programming language is incomplete without these two basic functionalities. Similarly, Java, as a programming language, also has functionalities that make Java programs take input and display the output. The output gets printed on the console using System.out.print() and System.out.println() functions. The input part is handled by the Scanner class. In this article, you will learn in detail about the input functionality of the Java language using Scanner in Java. Also, you’ll learn in detail about how to import Scanner in Java and about the different Scanner class methods in Java.
In this article, we cover:
Java.util is a package in Java that has a class called Scanner. This class is used to obtain input from the user during runtime. The Scanner input in Java should be of primitive data types, such as int, float, double, etc., and string types. As an example, to get the value of a variable declared as int type, the function used is nextint(). Similarly, for float type input, the function is nextfloat().
For Java Scanner string type and char type inputs, the functions used are nextLine() and next()charAt(0). Here, the next() function gives the next word, and charAt(0) gives the first character in the string. The scanner offers the easiest method to read the input; however, it is not the most efficient one in some cases.
As the Scanner is a class and we create objects for the class to use its methods, so is the case with the Scanner class using Scanner class methods in Java. But for the Scanner class, the predefined object is System. in. The object – System. in denotes the standard input stream in Java.
To use any class in Java, the respective package needs to be imported. Similar is the case with the Java Scanner class. The package of the Scanner class – java. util. The scanner needs to be imported to use the Scanner class. Given below is the class Scanner in Java syntax to import the package:
import java.util.Scanner;
The above import Scanner Java statement needs to be present in the code before we use the Scanner class.
Once the import of the package is done, the next step is to create the object of the Scanner class. Steps to create the Scanner objects are as follow:
Object to read from file
Scanner obj1 = new Scanner(File filename);
Object to read from the input stream
Scanner obj2 = new Scanner(inputStream inputStreamName);
Object to read input as String
Scanner obj3 = new Scanner(String s);
Refer to the below table to check the commonly used Scanner functions that help take input from the user along with its specific data type.
Scanner constructor in Java can take any object as input. The input can include the file object too, which lets the Java program take input from a file. Another such object for the Java Scanner class constructor is the inputStream.
The below table describes the Java Scanner class constructors. In this table, you will know how to take string input in Java using Scanner.
The below table contains the Scanner function in Java present under the Java Scanner class. A few functions in the table have two types of Scanner input in Java.
Scanner.findWithinHorizon(String pattern, int horizon)
It returns True, if the input in radix is interpreted as Big integer, else returns False.
Scanner.hasNextByte(int radix)
Scanner.hasNextShort(int radix)
Read the Scanner program in Java that asks the user to enter the name using the Scanner method in Java nextLine(). Below is the example code:
import java.util.*;
public class Scanner_Example1 {
public static void main(String args[]) {
Scanner inp = new Scanner(System.in);
System.out.print(“Please input your name: ”);
String custName = inp.nextLine();
System.out.print(“Customer name is “ + custName)
inp.close();
}
Output:
Please input your name: Peter Soni
Customer name is Peter Soni
public class Scanner_Example2 {
String str = “Scanner Example”;
Scanner sc = new Scanner(str);
System.outprintln(“String is “ + sc.nextLine());
sc.close();
System.out.println(“Enter Employee Details”);
System.out.print(“Enter employee name: ”);
String empName = inp.next();
System.out.print(“Enter age: ”);
int age = inp.nextInt();
System.out.print(“Enter salary: ”);
double sal = inp.nextDouble();
System.out.println(“Employee Details”);
System.out.print(“Employee name is: ” + empName);
System.out.println(“Employee age: “ + age);
System.out.println(“Employee salary: “ + sal);
String is Scanner Example
Enter Employee Details
Enter employee name: Peter John
Enter age: 34
Enter salary: 75000
Employee Details
Employee name: Peter John
Employee age: 34
Employee salary: 75000.00
Below code snippet asks user to enter two numbers and returns the sum:
class Scanner_Example3 {
public static void main(String[] args) {
System.out.println(“Enter first number: “);
int num1 = inp.nextInt();
System.out.println(“Enter second number: “);
int num2 = inp.nextInt();
int sum = num1 + num2;
System.out.println(“Sum of numbers is “ + sum);
Enter first number: 25
Enter second number: 20
Sum of numbers is 45
Scanner class can also be used to split the input tokens. The default delimiter is whitespace, but users can specify the delimiter of their choice while writing the Java code.
Below is the Java code snippet for Scanner delimiter usage:
public class ScannerDelimiterExample {
Scanner sc = new Scanner(“10, 12, 14, 16, 18, 20”);
sc.useDelimiter(“\\s*, \\s*);
while(sc.hasNext()) {
System.out.println(sc.nextInt());
10
12
14
16
18
20
Although, Scanner class is very useful to take the user input at run time, there are some drawbacks of using Scanner class in Java. Refer to the below shortcomings of using Scanner in Java:
This was all about the Java Scanner class. All the details about Scanner class in Java, such as the syntax with examples, different Scanner methods, Java Scanner constructor, and drawbacks of the Java Scanner class. You can read similar Java-based articles on Jigsaw Academy and enhance your Java knowledge. You can also check out Jigsaw’s Master Certificate in Full Stack Development if you want to learn Java in more detail.
Fill in the details to know more
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
History of Java Programming Language| UNext
November 18, 2022
An Advanced Guide to Shell Scripting in Unix!
November 15, 2022
Java Tutorial For Beginners
September 28, 2022
What Is Data Abstraction?
August 24, 2022
Encapsulation In Oops With Example
August 23, 2022
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile