Sitemap

JAVA INPUT(Scanner)

User Input

--

The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:

Press enter or click to view image in full size
Method 1 to import Scanner
Press enter or click to view image in full size
Method 2 to import Scanner
Press enter or click to view image in full size
Press enter or click to view image in full size
Output

Java Scanner class allows the user to take input from the console. It belongs to java.util package. It is used to read the input of primitive types like int, double, long, short, float, and byte. It is the easiest way to read input in Java program.

Methods of Java Scanner Class

In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at the table below:

Get AnjaliKesherwani’s stories in your inbox

Join Medium for free to get updates from this writer.

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Output

We can use Conditions & Loops

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
creating a table
Press enter or click to view image in full size
Output

Conclusion:

The java.util.Scanner class is a versatile and user-friendly tool for parsing input in Java applications. Its ability to read from various sources and handle different data types makes it invaluable for many programming tasks. While it is straightforward to use, developers should be cautious of its limitations in performance for large-scale data processing and ensure proper resource management by closing the Scanner object when done. Overall, Scanner is a fundamental class for efficient input handling in Java.

--

--

AnjaliKesherwani
AnjaliKesherwani

Written by AnjaliKesherwani

I possess a solid foundation in HTML, CSS, Bootstrap, JavaScript, MySQL, MongoDB, PHP, Laravel, Python, Django, Machine Learning, java, and MEAN Stack.