Home > Java > javaTutorial > body text

How to receive space separated input from user in Java?

WBOY
Release: 2023-08-20 23:50:50
forward
1365 people have browsed it

How to receive space separated input from user in Java?

Input and output are important parts of all programming languages, and Java is no exception. User input is very important for creating dynamic and interactive applications. Usually the input is a single value, but we can also get space-delimited input from the user. This article explains how to get space-delimited user input in Java.

Method to accept user input in space-separated format in Java

There are two ways to get space-delimited input from the user. They are as follows-

  • Use Scanner class

  • Use the BufferedReader class and then split and parse the individual values

Scanner class

Using the Scanner class is one way to get space-delimited input from the user. We will use the methods of Scanner class. It is located in the java.util package. Scanner class has many methods to get user input

Methods of Scanner class

Translated into Chinese:
nextInt() It is used to read and return the next integer value.
nextFloat() It is used to read and return the next floating point value.
nextDouble() It is used to read and return the next double value.
next() It is used to read and return the next word value as a string.
nextLine() It is used to read and return the next multiple word value as a string
nextLong() It is used to read and return the next long integer value.
nextShort()nextShort() It is used to read and return the next short value.
nextByte() It is used to read and return the value of the next byte.
nextBoolean() It is used to read and return the next Boolean value.

step

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

  • nextFloat() method will scan user input

  • The input will be stored in an array

Example

The following is an example of using the Scanner class in Java to get space separated input from the user.

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

Output

The 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
Copy after login
The Chinese translation of

Explanation

is:

Explanation

In the above code, we create a Scanner object. Since we want to get space-delimited input from the input, we use a loop that iterates 8 times, allowing the user to enter 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.

BufferedReader class

Using the BufferedReader class, the code reads a line of user input, splits it into individual values, and then parses the individual values. This is the most primitive way, the BufferedReader class is located in the java.io package.

readLine() method is used to read a line and store it as a string. If you need to convert a string to another data type, you must perform the type conversion explicitly.

step

    We will use the readLine() method in the BufferedReader class to scan the string containing spaces

    We will use the split() method to split the string by spaces

  • Now since the input is in the form of a string array, we can convert it if the required data type is different.

Example

The following is an example of using the BufferedReader class in Java to separate user input with spaces.

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

Output

The 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
Copy after login
The Chinese translation of

Explanation

is:

Explanation

In the above code, we created a BufferedReader class. Therefore, the readLine() method in the BufferedReader class can scan strings with spaces. We then split the string into individual values ​​and parse those values.

So we understand how to accept space separated input from the user in Java. Using Scanner class or BufferedReader class we can accept input as per our preference and needs. This allows us to create more flexible programs.

The above is the detailed content of How to receive space separated input from user in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template