🎓 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
Taking input from the user is a common requirement in many Java applications. Sometimes, the input might consist of multiple values separated by spaces, such as names, numbers, or other data. Java provides several ways to handle this, but one of the most straightforward methods involves using the Scanner class. This blog post will demonstrate how to take multiple inputs separated by space using the Scanner class in Java.
2. Program Steps
1. Import the Scanner class from the java.util package.
2. Create an instance of the Scanner class to read from System.in.
3. Prompt the user to enter data separated by spaces.
4. Use the nextLine() method of the Scanner instance to read the entire line of input.
5. Split the input string into individual tokens based on spaces.
6. Process and display the tokens.
3. Code Program
import java.util.Scanner;
public class UserInputSpaceSeparated {
public static void main(String[] args) {
// Step 1: Importing Scanner class and Step 2: Creating Scanner instance
Scanner scanner = new Scanner(System.in);
// Step 3: Prompting the user
System.out.println("Enter items separated by spaces:");
// Step 4: Reading the entire line of input
String inputLine = scanner.nextLine();
// Step 5: Splitting the input string into tokens
String[] items = inputLine.split(" ");
// Step 6: Processing and displaying the tokens
System.out.println("You entered:");
for (String item : items) {
System.out.println(item);
}
// Closing the scanner
scanner.close();
}
}
Output:
Enter items separated by spaces: apple banana cherry You entered: apple banana cherry
Explanation:
1. The program starts by importing the Scanner class, which is used to read the user's input from the standard input stream (System.in).
2. An instance of Scanner named scanner is created to read the input.
3. The user is prompted to enter items separated by spaces. This could be anything like words, numbers, etc.
4. The nextLine() method reads the entire line of input as a single string. This is necessary because the user's input could contain multiple items separated by spaces.
5. The input string is then split into individual tokens (items) using the split(" ") method. The space character is used as the delimiter.
6. The program iterates over the array of tokens using an enhanced for-loop and prints each item. This demonstrates how the input string was successfully separated into individual parts.
7. Finally, the Scanner instance is closed to prevent resource leaks. This is a good practice when working with classes that handle system resources.
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