如何使用 BufferedReader 对象从 Java 中的一行读取多个整数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25491997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read multiple integer values from one line in Java using BufferedReader object?
提问by Lalit Kumar
I am using BufferedReader class to read inputs in my Java program. I want to read inputs from a user who can enter multiple integer data in single line with space. I want to read all these data in an integer array.
我正在使用 BufferedReader 类来读取我的 Java 程序中的输入。我想从一个用户那里读取输入,该用户可以在一行中输入多个带空格的整数数据。我想在一个整数数组中读取所有这些数据。
Input format- the user first enters how many numbers he/she want to enter
输入格式——用户首先输入他/她想输入的数字
And then multiple integer values in the next single line-
然后在下一行中有多个整数值-
INPUT:
输入:
5
5
2 456 43 21 12
2 456 43 21 12
Now, I read input using an object (br) of BufferedReader
现在,我使用 BufferedReader 的对象 (br) 读取输入
int numberOfInputs = Integer.parseInt(br.readLine());
Next, I want to read next line inputs in an array
接下来,我想读取数组中的下一行输入
int a[] = new int[n];
But we cannot read using this technique
但是我们不能使用这种技术阅读
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine()); //won't work
}
So, is there any solution to my problem or we can't just read multiple integers from one line using BufferedReader objects
那么,我的问题是否有任何解决方案,或者我们不能使用 BufferedReader 对象从一行读取多个整数
Because using Scanner object we can read this type of input
因为使用 Scanner 对象我们可以读取这种类型的输入
for(int i=0;i<n;i++)
{
a[i]=in.nextInt(); //will work..... 'in' is object of Scanner class
}
采纳答案by Paul Vargas
Try the next:
尝试下一个:
int a[] = new int[n];
String line = br.readLine(); // to read multiple integers line
String[] strs = line.trim().split("\s+");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(strs[i]);
}
回答by Lalit Kumar
If you want to read integers and you didn't know number of integers
如果您想读取整数而您不知道整数的数量
String[] integersInString = br.readLine().split(" ");
int a[] = new int[integersInString.length];
for (int i = 0; i < integersInString.length; i++) {
a[i] = Integer.parseInt(integersInString[i]);
}
回答by Pramod
Late to the party but you can do this in one liner in Java 8 using streams
.
迟到了,但您可以在 Java 8 中使用streams
.
int[] input = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray()
;
int[] input = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray()
;
回答by VaibhavDomadia
You can use StringTokenizer class of java.util package. The StringTokenizer class allows an application to break a string into tokens. You can use this tokens using nextToken() method of StringTokenizer class.
您可以使用 java.util 包的 StringTokenizer 类。StringTokenizer 类允许应用程序将字符串分解为标记。您可以使用 StringTokenizer 类的 nextToken() 方法使用此令牌。
You can use following constructor of StringTokenizer:
您可以使用以下 StringTokenizer 的构造函数:
StringTokenizer(String str, String delimiter);
StringTokenizer(String str, String delimiter);
you can use space(" ") as delemeter.
您可以使用 space(" ") 作为 delemeter。
int a[] = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine() , " ");
for(int i=0 ; i<N ; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
回答by alok
import java.io.*;
public class HelloWorld{
public static void main(String []args){
int i;
System.out.println("enter the array element");
InputStreamReader isr= new InputStreamReader();
BufferedReader ib= new BufferedReader(isr);
int a[]=new int [5];
for(i=0;i<5;i++)
{
a[i]= Integer.parseInt(ib.readLine(a[i]));
}
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}
回答by ChillyWe
I use this code for List:
我将此代码用于列表:
List<Integer> numbers = Stream.of(reader.readLine().split("\s+")).map(Integer::valueOf).collect(Collectors.toList());
And it is almost the same for array:
数组几乎相同:
int[] numbersArray = Arrays.stream(reader.readLine().split("\s+")).mapToInt(Integer::valueOf).toArray();
回答by Debu Shinobi
Integer.parseInt(br.readLine())
-> Reads a whole line and then converts it to Integers.
Integer.parseInt(br.readLine())
-> 读取整行,然后将其转换为整数。
scanner.nextInt()
-> Reads every single token one by one within a single line then tries to convert it to integer.
scanner.nextInt()
-> 在一行中逐个读取每个标记,然后尝试将其转换为整数。
String[] in = br.readLine().trim().split("\s+");
// above code reads the whole line, trims the extra spaces
// before or after each element until one space left,
// splits each token according to the space and store each token as an element of the string array.
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(in[i]);
// Converts each element in the string array as an integer and stores it in an integer array.