How to Take Input From User Separated By Space in Java ? Last Updated : 01 Feb, 2023 Comments Improve Suggest changes Like Article Like Report There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each valueUsing nextInt( ) method of Scanner class Let us discuss both the methods one by one in order to get a better understanding by implementing the same clean java programs. Method 1: Using BufferedReader Class and then splitting and parsing each value. Procedure: Using readline() method of BufferedReader and Scan the whole string.Split this String for str.split(" ")Iterate over the above array and parse each integer value using Integer.parseInt( ). Example Java // Java Program to Take Input from User Separated by Space // Using BufferedReader class // Importing required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // Main class // BufferedReaderTest class GFG { // Main driver method public static void main(String[] args) throws IOException { // Creating an object of BufferedReader class BufferedReader bi = new BufferedReader( new InputStreamReader(System.in)); // Custom integer array of size 10 int num[] = new int[10]; // Array of string type to store input String[] strNums; // Display message System.out.println("enter string of numbers"); // Reading input a string strNums = bi.readLine().split(" "); for (int i = 0; i < strNums.length; i++) { num[i] = Integer.parseInt(strNums[i]); } // Display message System.out.println("printing stored numbers "); // Printing the stored numbers using for loop for (int i = 0; i < strNums.length; i++) { System.out.println(num[i]); } } } Output: FIG = OUTPUT OF METHOD 1 Method 2: Using nextInt() method of Scanner class. Procedure: Using the nextInt() method of Scanner class and scan to scan the input.Using for loop to store input in an array.Iterate through the above array and parse each integer using sc.nextInt() Example Java // Java Program to Take Input from User Separated by Space // Using Scanner class // Importing required classes import java.io.IOException; import java.util.Scanner; // Main class // Scanner class class GFG { // Main driver method public static void main(String[] args) throws IOException { // Display message for better readability System.out.println("enter input "); // Creating an object of Scanner class Scanner sc = new Scanner(System.in); // Declaring and initializing an array of size 10 int[] nums = new int[10]; int i; // Loop to store input values in nums array for (i = 0; i < nums.length; i++) { nums[i] = sc.nextInt(); } // Display message System.out.println("printing stored values"); // Printing stored values for (i = 0; i < nums.length; i++) { System.out.println(nums[i] + " "); } } } Output: FIG = OUTPUT OF METHOD 2 Note: The first method using Bufferedreader class and then splitting and parsing each value is much faster than using nixing() method of Scanner class. It is nearly 2 times faster than the second one. Below we do provide in order how to calculate the time consume by both methods by using nanotime method // Initializing variables long startTime, endTime; // Start time startTime = System.nanoTime(); { // Insert code here // Method 1 or method 2 code } // End time endTime = System.nanoTime(); Comment More infoAdvertise with us Next Article How to Take Input From User Separated By Space in Java ? K kamal251199 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads How to Take Array Input From User in Java? Arrays in Java are an important data structure, and we can add elements to them by taking input from the user. There is no direct method to take input from the user, but we can use the Scanner Class or the BufferedReader class, or the InputStreamReader Class.Different Ways to Take Array Input from U 6 min read How to Remove all White Spaces from a String in Java? In Java, to remove all white spaces from a String, there are several ways like using replaceAll() or manual iteration over characters. In this article, we will learn how to remove all white spaces from a string in Java.Example:We will use the replaceAll() method with the regex pattern "\\s" to remov 3 min read Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided 2 min read How to Split a String into Equal Length Substrings in Java? In Java, splitting a string into smaller substrings of equal length is useful for processing large strings in manageable pieces. We can do this with the substring method of the loop. This method extracts a substring of the specified length from the input string and stores it in a list.Example:In the 3 min read How to Pad a String in Java? Padding a String in Java involves adding extra characters, which may be spaces, zeros, or other given characters to the beginning or end of a string to meet a desired length. This is normally used in formatting outputs or aligning texts.Example:The String.format() method is a simple way to pad strin 3 min read How to Remove Duplicates from a String in Java? Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. In this article we have given a string, the task is to remove duplicates from it. Example Input: s = "geeks for geeks"Output: str = "geks for" Remove Duplicates From a 2 min read Java program to count the characters in each word in a given sentence Write a Java program to count the characters in each word in a given sentence? Examples: Input : geeks for geeksOutput :geeks->5for->3geeks->5 Recommended: Please solve it on PRACTICE first, before moving on to the solution. Approach:Here we have to find out number of words in a sentence an 3 min read How to Split a String in Java with Delimiter? In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter. The best example of this is CSV (Comma Separated Values) files.Program to Split a String with Delimiter in JavaHere is a simple program to split a string with a comma 2 min read How to Extract a Specific Line from a Multi-Line String in Java? In Java, String Plays an important role. As String stores or we can say it is a collection of characters. We want to get a specific line from a multi-line String in Java. This can also be done in Java. In this article, we will be learning how to extract a specific line from a multi-line String in Ja 2 min read Like