DK
Junior Member
Posts: 23
|
Post by DK on Nov 22, 2001 19:09:11 GMT -5
I've been trying to write a method to do input in this class. I import java.io.* and create a BufferedReader as usual but the thing is, we always had a main method with 'throws IOException' in the header. If the InputOutput class doesn't have a main method, how do I throw an IOException???Can anyone help me? ? :-/Thanks.
|
|
|
Post by Majin_Blues on Nov 22, 2001 20:05:50 GMT -5
hmm... my guess is you insert the "throws IOException" in the header of the method in which you are calling a readline or something...
public void getStuff(String a) throws IOException{ }
but that's a guess...
|
|
DK
Junior Member
Posts: 23
|
Post by DK on Nov 22, 2001 21:24:33 GMT -5
|
|
|
Post by pandachan on Nov 23, 2001 6:37:08 GMT -5
I had problem with that too. Finally I found a code that uses try and catch. It seems to work!
|
|
DK
Junior Member
Posts: 23
|
Post by DK on Nov 23, 2001 22:42:09 GMT -5
Just need some reassurance. Are we allowed to have throws IOException in the main method or would that count as doing I/O stuff where we're not supposed to? Concerning the try & catch stuff, how does it work?
|
|
|
Post by pandachan on Nov 24, 2001 8:04:18 GMT -5
I'm not sure how "try and catch" works...but as the website that I found says that there are 3 types of throw something stuff. And throw sth after the main method doesn't work if you wanna pass it to methods. try and catch is the one that works...ah...I dunno..but try..it works...
sth like this:
try { line = in.readLine(); } catch (IOException ioe) { }
try the line inside the "try" and if the IOException is caught, then do sth...so do nth(<== this is the above case). well...there might be another way to do this...does anyone did differently??
|
|
|
Post by Majin_Blues on Nov 25, 2001 16:39:12 GMT -5
why not throw in the "Throws IOException" in the main header anyway?
it won't affect the program even if you removed all bufferedreader stuff...
|
|
DK
Junior Member
Posts: 23
|
Post by DK on Nov 25, 2001 18:47:05 GMT -5
would it be ok then if I just added throws IOException to the main header and to the input method's header of the I/O class? :-/cuz it works without having to do the try & catch stuff.
|
|
|
Post by gundamf91 on Nov 25, 2001 19:58:44 GMT -5
all i did was (part of my code) this is a method not main
public void Input() throws IOException { ... } and it works fine
|
|
DK
Junior Member
Posts: 23
|
Post by DK on Nov 25, 2001 20:21:40 GMT -5
I think the reason why yours works is that you have 'void' instead of returning a value. When I replace 'void' with something like 'int', then I get an error (at main) saying that I need to be able to throw an IOException. all i did was (part of my code) this is a method not main public void Input() throws IOException { ... } and it works fine
|
|
|
Post by gundamf91 on Nov 25, 2001 20:24:13 GMT -5
i have other methods to return the values...my input method just does input and output...
|
|
DK
Junior Member
Posts: 23
|
Post by DK on Nov 25, 2001 20:42:21 GMT -5
oh.why didn't I think of that? It sure solves the problem I'm having. thanx.
|
|
|
Post by gundamf91 on Nov 25, 2001 20:43:16 GMT -5
oh my god i actually helped? hahha what a fluke i am ;D
|
|
DK
Junior Member
Posts: 23
|
Post by DK on Nov 25, 2001 20:53:32 GMT -5
umm... :-/just wondering... this is what the handout says: "When you want to read something from the keyboard, you will have to call a method in the input/output class which reads from the keyboard and returns the information to the calling method as a return value."
does this mean we must return the value inputted from the keyboard once we call the input method?
|
|
|
Post by gundamf91 on Nov 25, 2001 20:56:07 GMT -5
oh my god.....sigh....
|
|