Namit
Namit

Reputation: 1322

Using readline() and split()

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

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

  2. If the input is test t1 the key variable is supposed to be "test" (using System.out.println(key)) does that, but, it still doesn't enter the if 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    
       }
    }
    

I am not sure why this is happening, any help regarding the same will be much appreciated.

Upvotes: 0

Views: 15769

Answers (4)

Abhishek Oza
Abhishek Oza

Reputation: 3480

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().

On unix / Linux console end-of-file is [ctrl][d] and on DOS it is [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:

    BufferedReader reader = new BufferedReader(new FileReader("d:\\a1.txt"));

(assuming your input data is in file: "d:\a1.txt".)

Upvotes: 0

Reimeus
Reimeus

Reputation: 159774

The String text will never be null in this case. You can use:

while (!(text = reader.readLine()).isEmpty()) {

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

The loop does not end because a blank line causes readLine() to return an empty string, not null.

The comparison fails because Strings must be compared with equals() not ==

Upvotes: 1

PermGenError
PermGenError

Reputation: 46408

use equals() to check string equality.

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

String text = "";

thus your while loop would be an infinite loop

change

String text = ""; 
to
String text = null;

or if you wanna leave your text="" string as empty string.

use

while(!(text = reader.readLine()).isEmpty()) 

Upvotes: 3

Related Questions