0

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?

0

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.

0

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.

0

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?

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

return stream.read();

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

Edited by Reverend Jim: Fixed formatting

0

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?

Edited by mike_2000_17: Fixed formatting

1

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.)

0

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.

1

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()

0

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();
   }
0

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.

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.