I have a while loop that adds elements read using a Buffered Reader to an ArrayList. The loop works fine, except after it finishes, the app doesn’t seem to move on. The relevant code is:
int ctr = 1;
while((test = bf.readLine()) != null)
{
Log.i(TAG, test);
users.add(test);
Log.i(TAG, "" + (ctr++));
}
Log.i(TAG, "Loop done.");
The two log statements in the loop execute 4 times each, which is normal behavior. However, the statement after the loop doesn’t execute. It’s like it gets stuck. I’m pretty sure it doesn’t go into the loop either, as the two log statements inside don’t execute anymore either.
bf is the BufferedReader, users is the ArrayList.
LogCat Output:
08-04 01:35:37.472: I/UM(2937): UserInfo{0:Primary:3}
08-04 01:35:37.472: I/UM(2937): 1
08-04 01:35:37.476: I/UM(2937): UserInfo{1:Test1:0}
08-04 01:35:37.476: I/UM(2937): 2
08-04 01:35:37.476: I/UM(2937): UserInfo{2:test2:0}
08-04 01:35:37.480: I/UM(2937): 3
08-04 01:35:37.480: I/UM(2937): UserInfo{3:testxyz:0}
08-04 01:35:37.480: I/UM(2937): 4
Does anybody have any idea what my (probably stupid) mistake is?
I expect it’s the condition:
Think of it this way:
Your condition is checking for when Console closes its connection entirely, at which point BufferedReader will notice and return
null.What’s actually happening is that Console’s connection to BufferedReader is still open. BufferedReader is waiting for another
\nto get passed in from the Console beforereadLine()will return “line3\n”.