Java program to take input from user separated by space

There are 2 methods to require input from the user which are separated by space which are as follows:

1. Using BufferedReader Class then splitting and parsing each value
2. Using nextInt( ) method of Scanner class


Let us discuss both the methods one by one so as to urge a far better understanding by implementing an equivalent clean java programs.

Method 1: 

Using BufferedReader Class and then splitting and parsing each value.

Procedure:

  1. Using readline() method of BufferedReader and Scan the entire string.
  2. Split this String for str.split(” “)
  3. Iterate over the above array and parse each integer value using Integer.parseInt( ).

Example

 

 

// 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]);
		}
	}
}

Method 2: Using nextInt() method of Scanner class.

Procedure:

  1. Using the nextInt() method of Scanner class and scan to scan the input.
  2. Using for loop to store input in an array.
  3. Iterate through the above array and parse each integer using sc.nextInt()

Example

// 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 readibility
		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] + " ");
		}
	}
}

Note: The first method of using the Bufferedreader class and then splitting and parsing each value is much faster than using the nixing() method of the Scanner class. It is almost 2 times faster than the second one. Below we provide how to use the nano time method to calculate the time consumed by the two methods

// Initializing variables
long startTime, endTime;

// Start time
startTime = System.nanoTime(); {

// Insert code here
// Method 1 or method 2 code
}

// End time
endTime = System.nanoTime();

 

Submit Your Programming Assignment Details