Java User Input by (Scanner) class

What is Scanner Class in java computer program :
The `Scanner` class in Java, part of `java.util`, facilitates user input from keyboard, files, or strings. It offers methods to read various data types, supports delimiter customization, and resource management. It's crucial to handle exceptions and close the scanner to prevent resource leaks.
Here's a brief overview of how to use Scanner for user input:
- Import the Scanner class: You need to import the Scanner class at the beginning of your Java program:
import java.util.Scanner;
2. Create a Scanner object: Initialize a Scanner object to read input from the desired source. Typically, you'll use System.in for standard input (keyboard):
Scanner scanner = new Scanner(System.in);
3. Read user input: Use various methods provided by the Scanner class to read input. For example, nextLine() reads a line of text, nextInt() reads an integer, nextDouble() reads a double, etc.
System.out.print("Enter your name: ");
String name = scanner.nextLine();
4.Close the Scanner object: Once you're done reading input, it's good practice to close the Scanner object to release system resources:
scanner.close();
Example of Java Code :
import java.util.Scanner;
class sndp
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Your Name");
String name = scanner.nextLine();
System.out.println("Hello I am "+name);
scanner.close();
}
}
Here's a list of commonly used methods provided by the `Scanner` class for reading different types of input from the user:
1. nextLine(): Reads a line of text entered by the user, including whitespace characters, and returns it as a `String`.
2. nextInt(): Reads the next token entered by the user as an integer and returns it.
3. nextFloat(): Reads the next token entered by the user as a float and returns it.
4. nextDouble(): Reads the next token entered by the user as a double and returns it.
5. nextBoolean(): Reads the next token entered by the user as a boolean and returns it.
6. nextByte(): Reads the next token entered by the user as a byte and returns it.
7. nextShort(): Reads the next token entered by the user as a short and returns it.
8. nextLong(): Reads the next token entered by the user as a long and returns it.
9. hasNext(): Returns `true` if there is another token in the input, otherwise returns `false`.
10. hasNextLine(): Returns `true` if there is another line of input, otherwise returns `false`.
11. hasNextInt(): Returns `true` if the next token in the input can be interpreted as an integer, otherwise returns `false`.
12. hasNextFloat(): Returns `true` if the next token in the input can be interpreted as a float, otherwise returns `false`.
13. hasNextDouble(): Returns `true` if the next token in the input can be interpreted as a double, otherwise returns `false`.
14. hasNextBoolean(): Returns `true` if the next token in the input can be interpreted as a boolean, otherwise returns `false`.
15. hasNextByte(): Returns `true` if the next token in the input can be interpreted as a byte, otherwise returns `false`.
16. hasNextShort(): Returns `true` if the next token in the input can be interpreted as a short, otherwise returns `false`.
17. hasNextLong(): Returns `true` if the next token in the input can be interpreted as a long, otherwise returns `false`.
These methods provide flexibility for reading different types of input from the user in a Java program using the `Scanner` class.

Sandipan Kr Bag
I'm a dedicated full-stack developer, entrepreneur, and the proud owner of ocec.org.in , hailing from the vibrant country of India. My passion lies in creating informative tutorials and sharing valuable tips that empower fellow artisans in their journey. With a deep-rooted love for technology, I've been an ardent enthusiast of PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, Codeigniter, and Bootstrap from their earliest days. My philosophy revolves around the values of hard work and unwavering consistency, driving me to continuously explore, create, and share my knowledge with the tech community.
* Hire MeRelated Posts

জাভাস্ক্রিপ্ট কি? এটি কেন ব্যবহার করা হয় ?

জাভাস্ক্রিপ্ট লেখার পদ্ধতি
Step-by-Step Guide a Dynamic Image Slider with HTML, CSS, and JavaScript
Search
Latest Posts
Using AOS (Animate On Scroll) in React with Tailwind CSS
1 month ago

WebkitSpeechRecognition API
2 months ago

GitHub Understanding Common Prefixes in Conventional Commits
2 months ago
Subscribe to Our Newsletter
Get the latest updates straight to your inbox.