Dear all,

I have been told there is something wrong
with the following code. Could someone
please shed a light on this, I'd be greatly
thankful to your help.

VM
----------------------------------------------------

Identify the problem in the following code:

// Retry three times on failure to open file.
int retryCount = -1;
String line = null;
do {
    try {
        BufferedReader br = new BufferedReader(
            new InputStreamReader(new FileInputStream("input.txt")));
        line = br.readLine();
        br.close();
    } catch (IOException e) {

        // Wait a little bit.
        Thread.currentThread().sleep(250);

        if (retryCount == -1) {
            // Start the count at three if this is the first occurrence.
            retryCount = 3;
        } else {
            // Okay, looks like somebody has tried before.
            retryCount--;
        }
    }
} while (retryCount > 0);

This is your quiz (you could, at least, have left off the question which makes it undeniably a quiz/homework question).

What do you think is wrong with it.

This is your quiz (you could, at least, have left off the question which makes it undeniably a quiz/homework question).

What do you think is wrong with it.

Hi masijade,

Thanks for your response.
Acutally I got the quiz, I'm trying to find out what's
wrong with that, but my Java thread is not good enough
to trace it out. For me, I think I might need another
try/catch such as

Thread.currentThread().sleep(250);
try {
    Thread.sleep( 250 );
 }
  catch ( InterruptedException e ) {
    // the thread got interrupted for some reason
   // and then don't know what I need to do in this block
  }

but I'm really not sure. Could you please suggest some idea.

Thanks very much!
VM

Well, put it into a Class and try it out. Do the instructions say you actually need to do anything other than make it compilable (and remain functional, of course).

Well, put it into a Class and try it out. Do the instructions say you actually need to do anything other than make it compilable (and remain functional, of course).

Do you mean putting everything as a function in a class?
Thanks,
VM

This article has been dead for over six months. Start a new discussion instead.