How to Take Input from User Separated by Space in Java?


Input and output are the vital components of all the programming languages. Same is the case with Java. User input is very crucial for creating dynamic and interactive applications. Usually the input is a single value but we can also take input from the user separated by space. This article deals with how to take input from the user separated by spaces in Java.

Ways to Take Input From User Separated By Space in Java

There are 2 ways by which we can take the input from the user separated by space. They are as follows-

  • Using the Scanner Class

  • Using the BufferedReader class and then splitting and parsing individual values

The Scanner Class

Using the Scanner Class is one of the ways to take space-separated input from the user. We will use the methods of the Scanner class. It is present in the java.util package. The Scanner class has many methods to take user input.

Scanner Class Methods

nextInt() It is used to read and return the next integer value.
nextFloat() It is used to read and return the next float value.
nextDouble() It is used to read and return the next double value.
next() It is used to read and return the next one word value as String.
nextLine() It is used to read and return the next multiple word values as String
nextLong() It is used to read and return the next long value.
nextShort() It is used to read and return the next short value.
nextByte() It is used to read and return the next byte value.
nextBoolean() It is used to read and return the next Boolean value.

Steps

We will use the nextFloat() method here to understand how to take space separated inputs from the user. These steps can be applied to other methods mentioned above.

  • The nextFloat() method will scan the user input

  • The input will be stored in an array

Example

Following is an example to use the Scanner Class in Java to take the input from the user separated by space.

//import the java.util package
import java.util.*;
 
public class Main {
   public static void main(String args[])
   {
      Scanner scn= new Scanner(System.in);
      System.out.println("Enter 8 values ");
      
      float[] val = new float[8];
      
      //to store the input values
      for (int i = 0; i < val.length; i++) {
         val[i] = scn.nextFloat();
      }
      //print the values
      System.out.println("The values are ");
      for (int i = 0; i < val.length; i++) {
         System.out.println(val[i]);
      }
   }
}

Output

Following is the output of the above code 

Enter 8 values 
12 89 43 67 51 39 91 87
The values are 
12.0
89.0
43.0
67.0
51.0
39.0
91.0
87.0

Explanation

In the above code, we have created a Scanner object. Since we want to take space-separated inputs from the inputs we have used a loop to iterate 8 times, allowing the user to input the values one by one using the nextFloat() method of the Scanner class. Each input value is stored in the corresponding index of the val array.

The BufferedReader class

Using the BufferedReader class, the code takes user input by reading a line, splitting it into individual values, and then parsing individual values. It is the most primitive way and the BufferedReader class is present in the java.io package.

The readLine() method is used to read the line and store it as a String. If the String has to be converted to other data types, it has to be explicitly type casted.

Steps

  • We will be using the readLine() method present in the BufferedReader class to scan the string with spaces present.

  • We will split the string using the split() method with space as the argument.

  • Now since the input is in the form of an array of Strings, if the desired data type is different, we can convert it.

Example

Following is an example to use the BufferedReader Class in Java to take the input from the user separated by space.

//import the java.io package
import java.io.*;

class Main {

   public static void main(String[] args) throws IOException
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      //array
      float num[] = new float[8];

      System.out.println("Enter the string of values separated with spaces ");

          //split the string
      String[] val = br.readLine().split(" ");
      
      //parse individual values
      for (int i = 0; i < val.length; i++) {
         num[i] = Integer.parseInt(val[i]);
      }

      System.out.println("The values are ");

      //print
      for (int i = 0; i < val.length; i++) {
         System.out.println(num[i]);
      }
   }
}

Output

Following is the output of the above code -

Enter the string of values separated with spaces 
12 13 14 15 18 19 17 16
The values are 
12.0
13.0
14.0
15.0
18.0
19.0
17.0
16.0

Explanation

In the above code, we have created a BufferedReader class. So, the readLine() method present in the BufferedReader class scans the string with spaces present. We then split the string into individual values and then parse those values.

Hence, we comprehended how to take input from user separated by space in Java. Using the Scanner class or the BufferedReader class, we can take the inputs depending on our preference and requirements. This allows us to create more flexible programs.

Updated on: 24-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements