|
|
Subject:
FilteredInputStream in Java
Category: Computers > Programming Asked by: brianct-ga List Price: $8.00 |
Posted:
20 Dec 2003 17:09 PST
Expires: 19 Jan 2004 17:09 PST Question ID: 289135 |
I am reading the book java An object-oriented Approach by Arnow and Weiss and am wondering why a sample code that is provided in the book does not work. Please help me fix the code so it works and explain why it did not work (it is the exact code in the book). I am using NetBeans IDE 3.5.1 with J2SE 1.4.2. import java.net.*; import java.io.*; public class WHWWW { public static void main(String[] args) throws Exception { URL u = new URL("http://www.whitehouse.gov/"); FilterInputStream ins = u.openStream(); InputStreamReader isr = new InputStreamReader(ins); BufferedReader whitehouse = new BufferedReader(isr); System.out.println(whitehouse.readLine()); System.out.println(whitehouse.readLine()); } } |
|
Subject:
Re: FilteredInputStream in Java
Answered By: endo-ga on 20 Dec 2003 17:53 PST Rated: |
Hi, Thank you for your question. Here is the correct code. You will see that the change is very minor: package counting; import java.net.*; import java.io.*; public class WHWWW { public static void main(String[] args) throws Exception { URL u = new URL("http://www.whitehouse.gov/"); FilterInputStream ins = (FilterInputStream)u.openStream(); InputStreamReader isr = new InputStreamReader(ins); BufferedReader whitehouse = new BufferedReader(isr); System.out.println(whitehouse.readLine()); System.out.println(whitehouse.readLine()); } } You need specifically cast the InputStream into a FilterInputStream, that's what (FilterInputStream) does. The Java compiler does not allow you to use FilterInputStream where InputStream is expected. There are other (more complicated) solutions provided on: javajunkies http://www.javajunkies.org/index.pl?lastnode_id=458&node_id=1034 I hope this answers your question. If anything is unclear or you require any clarifications, please do not hesitate to ask. Thanks. endo | |
| |
|
brianct-ga
rated this answer:
and gave an additional tip of:
$2.00
Thanks for the help. |
|
Subject:
Re: FilteredInputStream in Java
From: eadfrith-ga on 20 Dec 2003 17:55 PST |
I checked the errata for the book and sure enough there is a problem with the code you were trying to use. The errata can be found here: ftp://ftp.aw.com/cseng/authors/arnow/Java2SourceCode/Errata.txt The relevent entry states: --------- Chapter 3 --------- - page 79- The WHWWW class contains two typos: - A 'throws Exception' is missing from the signature of the 'main' method - The declaration: FilterInputStream ins = u.openStream(); shoud read: InputStream ins = u.openStream(); - The online source code contains the correct version. The online code can be found here: ftp://ftp.aw.com/cseng/authors/arnow/Java2SourceCode/ The corrected code is: import java.net.*; import java.io.*; class WHWWW { public static void main(String[] arg) throws Exception { URL u = new URL("http://www.whitehouse.gov/"); InputStream ins = u.openStream(); InputStreamReader isr = new InputStreamReader(ins); BufferedReader whiteHouse = new BufferedReader(isr); System.out.println(whiteHouse.readLine()); System.out.println(whiteHouse.readLine()); System.out.println(whiteHouse.readLine()); System.out.println(whiteHouse.readLine()); System.out.println(whiteHouse.readLine()); } } The problem with the original code is that it tries to assign the object returned by the openStream method of the URL class, which in the API is declared to be of type InputStream, to a variable of type FilterInputStream. The relevent line is: FilterInputStream ins = u.openStream(); This will cause a compile time error, since it attempts a narrowing conversion without a cast. In plane English this means that you're trying to store an object of a given class in a variable that's declared to be a more specific class. If you knew that the object returned was in fact a FilterInputStream, or a subclass, then you could correct the code by adding a narrowing cast, as in: FilterInputStream ins = (FilterInputStream )u.openStream(); This will now compile. However, it's not correct, since we don't know, and shouldn't care, what the actual type of the object returned is. In fact if you were to check, by printing the class of the object, you'd find it was an instance of the HttpURLConnection class. Hope this helps. Cheers, Eadfrith |
Subject:
Re: FilteredInputStream in Java
From: eadfrith-ga on 21 Dec 2003 10:28 PST |
I don't believe the problem is with your code but with the java compiler getting confused by the spaces in the location of the file. I've not used netbeans, but it appears that when you try to compile your class it's issuing the command: javac C:\Documents and Settings\brian\.netbeans\3.5\sampledir\WHWWW.java which is fine, except that javac sees the spaces in the "Document and Settings" and breaks up the command line. This is an issue under windows. How to fix it? If this was the command line you could simple put quotes around the filename, as in javac "C:\Documents and Settings\brian\.netbeans\3.5 \sampledir\WHWWW.java" but since netbeans is issuing the compile command I'm not sure how to cause it to do this. The simpler solution would be to create your project in a location other than "Documents and Settings\brian", which is the actual location of the "My Documents" folder under windows. Create it in a location which doesn't have any spaces and you should be fine. Cheers, Eadfrith |
Subject:
Re: FilteredInputStream in Java
From: endo-ga on 21 Dec 2003 16:24 PST |
Thank you for the great rating and generous tip. Regards, endo |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |