java BufferedReader 空间分隔输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30614607/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:22:43  来源:igfitidea点击:

BufferedReader space separated input

javainputbufferedreader

提问by shaythan

first I'd like to mention that I am not realy experienced in java, and I searched StackOverFlow for a solution to my problem and either I didn't find it or didn't understand the answer, so I am asking now:

首先我想提一下,我在 Java 方面并没有真正的经验,我在 StackOverFlow 上搜索了我的问题的解决方案,但我没有找到它或不明白答案,所以我现在问:

i wanted to start working with BufferedReader and didn't find any guide that i understood propely, so i picked up bits from here and there and wrote this example :

我想开始使用 BufferedReader 并且没有找到任何我理解的指南,所以我从这里和那里拿起了一些东西并写了这个例子:

BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
int x = Integer.parseInt(input.readLine());
String y = input.readLine();
System.out.println(x);

this code worked for the input 34then enter then abc, but at what im trying to achieve i need the input 34 abcseparated by space to be inputed together and that xwill get 34and ywill get abc. this will work when using Scanner, but the problem is Scanner times out the exercise i'm doing because it's slow.

这段代码适用于输入34然后输入然后输入abc,但是在我试图实现的目标中,我需要将34 abc空格分隔的输入一起输入,然后x将得到34并且y将得到abc。这在使用 Scanner 时会起作用,但问题是 Scanner 使我正在做的练习超时,因为它很慢。

is there any simple way to get those input space separated like it was with Scanner?

有没有什么简单的方法可以像使用 Scanner 一样将这些输入空间分开?

回答by Sanira

Try this,

试试这个,

StringTokenizer tk = new StringTokenizer(input.readLine());
int m = Integer.parseInt(tk.nextToken());
String s = tk.nextToken();

this is faster than string.split();

这比 string.split() 快;

回答by zubergu

If you want to read 2 values from the same line you can't parse the whole line to Integer. This gives NumberFormatException.

如果要从同一行读取 2 个值,则无法将整行解析为 Integer。这给出了 NumberFormatException。

First read the line, then split it on ' ', and then parse 1st part to Integer.

首先阅读该行,然后将其拆分为 ,然后将' '第一部分解析为整数。

String line = reader.readLine();
String[] splitLine = line.split(" ");
Integer x = Integer.parseInt(splitLine[0]);
String y = splitLine[1];

回答by Uma Kanth

String line = input.readLine();
String []tokens = line.split(" ");
int x = Integer.parseInt(tokens[0]);
String y = tokens[1];

Split the input with split()function and access it from the array

使用split()函数拆分输入并从数组中访问它

回答by Codebender

Were you not able to use String.split()function on the input.readLine()?

你不能在 上使用String.split()函数input.readLine()吗?

String line = input.readLine();
int x = Integer.parseInt(line.split(" ")[0]);

String line = input.readLine();
int x = Integer.parseInt(line.split(" ")[0]);

回答by Kasumi Gunasekara

You can read multiple integers in the same row separated by spaces like: 1 5 5; by using the following example.

您可以读取同一行中由空格分隔的多个整数,例如:1 5 5; 通过使用以下示例。

StringTokenizer tk = new StringTokenizer(input.readLine());
int a = Integer.parseInt(tk.nextToken());
int b = Integer.parseInt(tk.nextToken());
int c = Integer.parseInt(tk.nextToken());