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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:00:02  来源:igfitidea点击:

Using readline() and split()

javasplitbufferedreaderreadline

提问by Namit

The code below is mostly self explanatory. However, I am having trouble in two cases:

下面的代码主要是不言自明的。但是,我在两种情况下遇到了麻烦:

  1. The whileloop does not exit even with the command line is left blank.

  2. If the input is test t1the keyvariable is supposed to be "test" (using System.out.println(key)) does that, but, it still doesn't enter the ifcondition 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    
       }
    }
    
  1. while即使命令行留空,循环也不会退出。

  2. 如果输入test t1key变量应该是“测试”(使用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 textwill never be nullbecause 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 Stringtextwill never be nullin this case. You can use:

Stringtext绝不会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()) != nullto (text = reader.readLine()) != null && !text.isEmpty()is because readLine()returns nullwhen 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 nullbefore 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 readerwill 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"。)