🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
The Scanner class in Java is a simple text scanner that can parse primitive types and strings using regular expressions. It breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The nextLine() method is particularly useful when you must capture an entire line of input, including spaces, which is impossible with methods like next() that only read input until the space.
2. Program Steps
1. Import the Scanner class.
2. Create a new Scanner instance to read from System.in.
3. Prompt the user to enter a line of text.
4. Use the nextLine() method to read the entire line of text.
5. Close the scanner to prevent resource leaks.
3. Code Program
import java.util.Scanner;
public class ScannerNextLineExample {
public static void main(String[] args) {
// Step 1: Creating a Scanner object to read input from standard input stream
Scanner scanner = new Scanner(System.in);
// Step 2: Prompting the user to enter a line of text
System.out.println("Enter a line of text:");
// Step 3: Reading the entire line of text entered by the user
String line = scanner.nextLine();
// Step 4: Displaying the line of text entered by the user
System.out.println("You entered: " + line);
// Step 5: Closing the scanner
scanner.close();
}
}
Output:
(Example output based on user input "Hello, this is a test.") Enter a line of text: You entered: Hello, this is a test.
Explanation:
1. A Scanner object named scanner is created to read text from the System.in input stream, allowing the program to read input provided by the user through the console.
2. The user is prompted to enter a line of text. This instruction is displayed on the console using System.out.println.
3. The nextLine() method is called on the scanner object to read an entire line of input from the user. Unlike next(), nextLine() captures everything up to the end of the line, including spaces.
4. The text entered by the user is then printed to the console, showing the use of nextLine() for capturing and echoing full lines of input.
5. Finally, the scanner is closed using the close() method to prevent resource leaks. It's a good practice to close the Scanner object when it's no longer needed.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment