You read from 'stream'. That is your InputStream object that is being passed into the method.

You read from 'stream'. That is your InputStream object that is being passed into the method.

Okay, silly me.

That's sorted, but when i compile it, it highlights the "if (number != null)" and says "cannot find symbol - variable number".

This is weird, i've initialised number to zero and everything, but why can't it find it?

You defined number in the try block. It is out of scope in the finally block. Even if it weren't, it's the stream that you want to check for null - not the number.

You defined number in the try block. It is out of scope in the finally block. Even if it weren't, it's the stream that you want to check for null - not the number.

Alright. Here's my code now...

public int countNo(InputStream stream)throws IOException
   {
       
       try {
           int number = 0;

            while ((number = stream.read()) != -1) {
                number++;
            }

        } finally {
            if (stream != null) {
                stream.close();
            }
        }
        return stream.read();
   }

Let me test it.

Well it compiled and works.

But there's an error (in my course application) that says...

Your result was null
It should have been 1869
Try again!

Do you know what i could do to sort this out?

if (stream != null) {
    stream.close();
}

return stream.read();

Why would you close the stream, then read from it? ....

start quote:

if (stream != null) {
                stream.close();
}

return stream.read();

Why would you close the stream, then read from it? ....

Okay, so if i remove "stream.close();", will the i get my result as a int instead of a null?

One test program is worth a thousand expert opinions.

(Not that I'm an expert, because frankly, I'm not. But it's the truth.)

One test program is worth a thousand expert opinions.

(Not that I'm an expert, because frankly, I'm not. But it's the truth.)

Well i removed it and i get a similar error saying...

Your result was -1
It should have been 1854
Try again!

Could it be the "if (stream != null)"?

I'm not quite sure what to do here.

The value of 'number' is what you are wanting to return, since that is your counter variable. Of course, you will have to declare it above the try-catch block to do so... and not reset it every read iteration with this part number = stream.read()

The value of 'number' is what you are wanting to return, since that is your counter variable. Of course, you will have to declare it above the try-catch block to do so... and not reset it every read iteration with this part number = stream.read()

Alright, i want to ask some questions....

1) When i remove the int number = 0; from the try-catch block, what will then go into that?

2) Do i know have to remove number = stream.read() now?

Sorry if these questions are dumb, i just want to make sure that i'm going in the right direction.

Will it be something like this...

public int countNo(InputStream stream)throws IOException
   {
       int number = 0;
       try {
           

            while ((stream.read()) != -1) {
                number++;
            }

        } finally {
            if (stream != null) {
                
            }
        }
        return stream.read();
   }

1) I said move, not remove.
2) Yes, unless you want 'number' to be set to the number of bytes read on each invocation of that statement in your loop - which negates the whole point of incrementing it inside the loop as a counter.

1) I said move, not remove.
2) Yes, unless you want 'number' to be set to the number of bytes read on each invocation of that statement in your loop - which negates the whole point of incrementing it inside the loop as a counter.

Okay, thanks.

I've sorted it out and it's now working and giving me the result i want.

Thanks to all the people who helped me with this...

masijade, Ezzaral, JamesCherrill, BestJewSinceJC, abbie and alias120.

I appreciate it.

Dear JamesKox,

Your code is itself a trouble. If your stream is buffered or FileStream or ByteArrayStream then you may use available() method to return the count of unread bytes.