1

I have a chunk of code from a simple chat application, but that is not the important part of this question. It is the part of code which, as it seems to me, should be unreachable:

while (!end) {
            outputToServer.println(consoleInput.readLine());
        }

        communicationSocket.close();

    } catch (Exception e) {
        // TODO: handle exception
    }
}

@Override
public void run() { // receiving message from other clients

    String serverTextLine;

    try {
        while ((serverTextLine = inputFromServer.readLine()) != null) {
            System.out.println(serverTextLine);

            if (serverTextLine.indexOf("*** Goodbye") == 0) {
                end = true;
                return;
            }
        }

    } catch (Exception e) {

    }

}

What I don't understand is how will a program ever reach a part of code in which it sets "end" variable to true, when the while loop which uses it as a condition is before it... I suppose it is some basic java stuff that I don't remember, or something I persistently overlook :) Help, please?

dzenesiz
  • 1,388
  • 4
  • 27
  • 58
  • 2
    The most obvious suggestion is that some other thread hits the off switch here, setting "end" to true. The other obvious suggestion is that you'd have an "end" set in the to-be-written catch block. (ie, if you lose the connection, just send "end" and exit the loop) – Jon Kiparsky Dec 23 '14 at 01:51
  • 1
    It's not "before" it. Those while-loops are in different methods, and if you're overriding `run()` you're probably doing some threading. – azurefrog Dec 23 '14 at 01:54
  • 1
    Yeah, the existence of `@Override` above the `run` method is a sign that this is class is either a `Thread`, or a `Runnable` that will be used by a thread. So this is almost certainly multi-threaded code, where one thread sets `end` and another checks it. – Dawood ibn Kareem Dec 23 '14 at 01:57
  • @JonKiparsky your displeasure is well justified, but this was an example of network programming, so that was overlooked on purpose. I am new to threads and threading and am still getting to know the concept, so I believe you are right about that other thread. You all are. Thank you! – dzenesiz Dec 23 '14 at 02:01

1 Answers1

1

as the code says the control will reach the line

end = true;

when the condition

serverTextLine.indexOf("*** Goodbye") == 0

returns true!,

that is the method indexOf(String) returns : the index of substring within the string if existing, and return -1 if not found!

The case to get "0" as index is only when the string starts with the substring. ie, when serverTextLine starts with "*** Goodbye".

Blue_Alien
  • 2,148
  • 2
  • 25
  • 29
  • Yes, but what I failed to understand is how will it ever reach the .indexOf, when it is constantly going through the loop BEFORE the part of code in which it actually changes the looping condition :) – dzenesiz Dec 23 '14 at 15:45
  • 1
    the program will print some string as output whenever the input is not null.!! the while loop checks whether the input string is null or not, which proceeds and prints the string if it is not null. then it will check the indexOf input string to special word, here goodbye!! and if it is the special word, then the thread itself stops execution as it returns. – Blue_Alien Dec 23 '14 at 16:19