Click to See Complete Forum and Search --> : passing objects?
Dillinger4
Jan 3rd, 2001, 10:26 AM
Hello everyone! I hope everyones holiday was good.
Im having a little trouble passing an object and im not
to sure as to what i an doing wrong. Instead of bogging you down with a lot of code from my program ill just give you a general idea of what im trying to do with a little code snippet.
import java.io.*;
class myclass{
public static void main(String[] args){
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
//The "standard" input stream System.in is represented by the variable stdIn
menu(stdIn); // function call
} // end main
public static void menu(Object stdIn){
String choice = "";
System.out.println("Enter a choice");
choice = (stdIn.readLine());
if(choice.equals("C") || choice.equals("c"))
}
} // end class
// i need to pass stdin to the to menu method so i can use the readLine() function but with what i have i get compile
errors. any suggestions would be great!
thanks
The class "Object" does not have the method "readLine()".
1)If you know that you are passing a class BufferedReader, then the signature of the menu method should be:
public static void menu(BufferedReader stdIn)
If you don't know the class of the object you are passing, then you might want to use the "instanceof" operator to test that you indeed have passed a BufferedReader and then cast stdIn to a class BufferedReader:
BufferedReader br = null;
SomeOtherClass soc = null;
if(stdIn instanceof BufferedReader)
br = (BufferedReader)stdIn;
else if(stdIn instanceof SomeOtherClass)
soc = (SomeOtherClass)stdIn;
2) You can have your methods throw an exception
public static void menu(BufferedReader stdIn) throws IOException{
even the main method, but it is usually better to use a try block to handle the appropriate exception (especially if you will be passing different type objects).
3) Check out the method "equalsIgnoreCase" of class String
if(choice.equalsIgnoreCase("c")
Dillinger4
Jan 4th, 2001, 07:13 PM
Hey thanks VirtuallyVB i knew what i was passing was
an Object but i forgot that i had to pass what is
being represented by BufferedReader. Also you said that
the "instanceof" operator can be used to test if something
is an instance of class and then i can be cast like so
BufferedReader br = null;
SomeOtherClass soc = null;
if(stdIn instanceof BufferedReader)
br = (BufferedReader)stdIn;
else if(stdIn instanceof SomeOtherClass)
soc = (SomeOtherClass)stdIn;
so is this neither a widening conversion or a narrowing
conversion? just curious. {{{laughing}}}
Thanks again......
Had I taken the Java Certification two months ago when I was borrowing a friend's book, I'd be confident that this is a narrowing conversion. Now I'm just guessing that it is one. I'm waiting on getting my own book to study for the certification.
No Joke.
It's a career move that may be required within 90 days.
[Edited by VirtuallyVB on 01-05-2001 at 12:08 AM]
Dillinger4
Jan 5th, 2001, 04:51 PM
Ive been studying for the java cert test with a friend every weekend and it doesnt seem to bad, but the developer and architect tests seem harder. I just bought Java 2
certification hardcover from new riders which covers exams 310-025, 310-027 and 310-050 and it seems pretty good so far.
It came with a what they call "New Riders Java test Engine" Which they say simulates the architect and programmer exams,times your exam, grades your answers, and recommends further study if necssary. Contains more than 450 questions and 20 exams that thoroughly prepare you for certification. So i figured what the heck. Ill try it.
What's another $39.99 anyway? {{{laughing}}}
I wasn't clear. I'm waiting to receive my shipment of Java books which includes a certification book. I'm disappointed in this internet shipment delay with the holidays and all.
Had I been more ambitious, I probably would have had my certification about two months ago. I was scoring in the 80's on the sample tests and I wanted to be scoring in the 90's before taking the real test.
Did you find out if it was a "narrowing conversion" or not?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.