To take input from the user separated by space in Java, you can use the Scanner class which provides various methods for reading input from different sources including the keyboard. Here's an example of how to use Scanner class to take input from the user separated by space in Java:
import java.util.Scanner; public class InputSeparatedBySpace { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter values separated by space: "); String input = scanner.nextLine(); String[] values = input.split(" "); // Splitting input into an array of values separated by space System.out.println("Values entered by user:"); for (String value : values) { System.out.println(value); } scanner.close(); } }
In the above code, we first create an instance of the Scanner class and pass System.in
as a parameter to the constructor to read input from the keyboard. Then, we prompt the user to enter values separated by space using the System.out.print
method, and read the entire line of input using the nextLine()
method of the Scanner class.
After that, we split the input string into an array of values separated by space using the split()
method of the String class. Finally, we loop through the array and print each value entered by the user using the System.out.println
method.
Note: It is important to close the Scanner object using the close()
method once you are done using it to avoid resource leaks.
https://programmingshark.com/c-sharp-programming-assignment-help.php
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.