java 使用 readline() 和 split()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13242218/
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
Using readline() and split()
提问by Namit
The code below is mostly self explanatory. However, I am having trouble in two cases:
下面的代码主要是不言自明的。但是,我在两种情况下遇到了麻烦:
The
while
loop does not exit even with the command line is left blank.If the input is
test t1
thekey
variable is supposed to be "test" (usingSystem.out.println(key)
) does that, but, it still doesn't enter theif
condition for some reason.String[] broken_text = null; String text = ""; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while((text = reader.readLine()) != null) { broken_text = text.split(" "); String first_key = broken_text[0]; if (first_key == "test") { //some statements } }
while
即使命令行留空,循环也不会退出。如果输入
test t1
的key
变量应该是“测试”(使用System.out.println(key)
)这样做,但是,它仍然没有进入if
某种原因条件。String[] broken_text = null; String text = ""; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while((text = reader.readLine()) != null) { broken_text = text.split(" "); String first_key = broken_text[0]; if (first_key == "test") { //some statements } }
I am not sure why this is happening, any help regarding the same will be much appreciated.
我不确定为什么会发生这种情况,对此的任何帮助将不胜感激。
回答by PermGenError
use equals() to check string equality.
使用 equals() 检查字符串是否相等。
if (first_key == "test") {
//some statements
}
should be
if (first_key.equals("test")) {
//some statements
}
your text
will never be null
because you declared it as
你text
永远不会是null
因为你宣布它为
String text = "";
thus your while loop would be an infinite loop
因此你的 while 循环将是一个无限循环
change
改变
String text = "";
to
String text = null;
or if you wanna leave your text=""
string as empty string.
或者如果您想将text=""
字符串保留为空字符串。
use
利用
while(!(text = reader.readLine()).isEmpty())
回答by Jim Garrison
The loop does not end because a blank line causes readLine()
to return an empty string, not null
.
循环不会结束,因为空行导致readLine()
返回空字符串,而不是null
。
The comparison fails because Strings must be compared with equals()
not ==
比较失败,因为字符串必须与equals()
not进行比较==
回答by Reimeus
The String
text
will never be null
in this case. You can use:
该String
text
绝不会null
在这种情况下。您可以使用:
while (!(text = reader.readLine()).isEmpty()) {
回答by Abhishek Oza
this should be your edited code:
这应该是您编辑过的代码:
String[] broken_text = null;
String text = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while((text = reader.readLine()) != null && !text.isEmpty()) {
broken_text = text.split(" ");
String first_key = broken_text[0];
if ( "test".equals(first_key)) {
//some statements
}
}
The reason changed (text = reader.readLine()) != null
to (text = reader.readLine()) != null && !text.isEmpty()
is because readLine()
returns null
when it encounters end-of-file as the first character, and it returns "" (empty string) when the first character is encounters is \r
(carriage return), \n
(line feed) , or \r\n
(carriage return followed by line feed). And you must always check for null
before checking for isEmpty()
.
改变的原因(text = reader.readLine()) != null
到(text = reader.readLine()) != null && !text.isEmpty()
是因为readLine()
返回null
,当它遇到结束文件作为第一个字符,并将它返回“”(空字符串)时的第一个字符是遭遇是\r
(回车), \n
(换行),或\r\n
(回车后跟换行符)。并且您必须始终null
在检查之前检查isEmpty()
.
On unix / Linuxconsole end-of-file is [ctrl][d]
and on DOSit is [ctrl][z]
在unix / Linux控制台文件结尾是[ctrl][d]
,在DOS上是[ctrl][z]
Note: In case you want to read input from a file (where you are more likely to get an end-of-file) instead of console, then your reader
will be initialised like this:
注意:如果您想从文件(您更有可能获得文件结尾)而不是控制台读取输入,那么您reader
将像这样初始化:
BufferedReader reader = new BufferedReader(new FileReader("d:\a1.txt"));
(assuming your input data is in file: "d:\a1.txt".)
(假设您的输入数据在文件中:"d:\a1.txt"。)