spatel spatel - 1 year ago 73
Java Question

How to correctly write While loop headers?

While loop events may be sentinel, flag, counter, or EOF. Using num as my variable, what is the correct way to write these. This is what i have so far, please correct if wrong.

  1. Flag:
    while (num <=5)

  2. sentinel:
    while (num !=5)

  3. counter:
    while (num=0; num < 5; num++).

  4. Im not sure how to write the header for the EOF.

Thank you for all the help. It is greatly appreciated.

Answer Source

All IO calls will usually fail when there is not and will not be any more data to read. If you're using an InputStream to read into a byte buffer, your basic loop will look like:

InputStream in = …;
byte[] buffer = new byte[512];
int read = -1;
while ((read = != -1) {
    // process contents of buffer

Since the documentation for[]) states:


the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download