• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with this method...  RSS feed

 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm having problem with this method i'm creating...



I'm creating a method that takes an InputStream as an parameter, reads the stream and returns the number of characters it contains as an int.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a method that takes an InputStream as an parameter, reads the stream and returns the number of characters it contains as an int.


Well, you method doesn't take an InputStream as a parameter -- it takes a byte array as the parameter. And if you want to know how many bytes are in a byte array, you can always used the length field of the array.

Henry
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Well, you method doesn't take an InputStream as a parameter -- it takes a byte array as the parameter. And if you want to know how many bytes are in a byte array

How do i go about making it take a InputStream as a parameter?

Henry Wong wrote:you can always used the length field of the array.

Can you show me an example of that?
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^^^^^^
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Barnard wrote:
Henry Wong wrote:Well, you method doesn't take an InputStream as a parameter -- it takes a byte array as the parameter. And if you want to know how many bytes are in a byte array

How do i go about making it take a InputStream as a parameter?



This is the line of code that declares the read method to take a byte[] as a parameter. How do you think you would change the declaration so that it took an InputStream instead ?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, your question isn't clear. Are you asking how to write a method which has an InputStream as its one and only parameter?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Sorry, your question isn't clear. Are you asking how to write a method which has an InputStream as its one and only parameter?


Are you referring to my question or Karl's question ?
If mine, that was just a rhetorical question intended as a hint to Karl as to what he needs to do.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you import BufferedReader? Is there a BufferedReader ctor that takes an InputStream? Did you *not* have that compilation issue before?!
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Did you import BufferedReader?

Yes

David Newton wrote:Is there a BufferedReader ctor that takes an InputStream?

I'm not sure, can you explain this a bit more?

David Newton wrote:Did you *not* have that compilation issue before?!

Yes, i did.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the JavaDocs for BufferedReader: you can't just pass arbitrary objects to ctors; there has to be a ctor defined for the object class you're trying to pass.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl, I would appreciate it if you could read this FAQ entry:

BeForthrightWhenCrossPostingToOtherSites

and act accordingly. Thanks in advance for your cooperation.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Karl, I would appreciate it if you could read this FAQ entry:

BeForthrightWhenCrossPostingToOtherSites

and act accordingly. Thanks in advance for your cooperation.


Okay sorry about that.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going back and deleting your comments seems a bit odd, and make a lot of the thread nonsensical.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Going back and deleting your comments seems a bit odd, and make a lot of the thread nonsensical.


Hi David Newton.

I got the code working now, that was a added reason why i deleted (sorry if that was wrong).
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not necessarily "wrong", but it's selfish, as is sending me personal messages after I provided you the FAQ link explaining why you shouldn't.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay sorry about that.

You said...

Where in your code do you do a mathematical operation? What value are you trying to calculate?


It's here

So the value i'm assuming is "number".

If I set the return as...

or

I get an error saying it can't find number.

Know what i'm doing wrong?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you defining "number"? Is it in a scope visible to the return statement?
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Where are you defining "number"? Is it in a scope visible to the return statement?

Oh, it's not.

What other way can i get an int result instead of a null?

I'm stuck on this one, the code looks right, but i honestly can't see where i'm going wrong.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, it's not.

Perhaps you could write the method in such a way that it is.
 
Karl Barnard
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Oh, it's not.

Perhaps you could write the method in such a way that it is.


No problem. I've sorted it out now David Newton.

Thank you very much and sorry for being such a pain in the ass!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a pain in the ass at all--learning stuff takes time!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!