如何在Java由空格分隔的用户输入?
有两种方法可以从用户那里获取输入,它们之间用空格分隔,如下所示:
- 使用 BufferedReader 类,然后拆分和解析每个值
- 使用 Scanner 类的 nextInt() 方法
让我们一一讨论这两种方法,以便通过实现相同的干净Java程序来更好地理解。
方法一:
使用 BufferedReader 类,然后拆分和解析每个值。
程序:
- 使用 BufferedReader 的readline() 方法并扫描整个字符串。
- 为 str.split(" ") 拆分此字符串
- 迭代上述数组并使用 Integer.parseInt() 解析每个整数值。
例子
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]);
}
}
}
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] + " ");
}
}
}
输出:
方法二:使用 Scanner 类的nextInt() 方法。
程序:
- 使用 Scanner 类的nextInt() 方法和 scan 来扫描输入。
- 使用 for 循环将输入存储在数组中。
- 遍历上述数组并使用 sc.nextInt() 解析每个整数
例子
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] + " ");
}
}
}
输出:
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. Bel;ow 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();