Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: i want my code to read the content of a html page... and am getting this error.

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i want my code to read the content of a html page... and am getting this error.

    Exception in thread "main" java.lang.RuntimeException:



    Uncompilable source code - cannot find symbol
    symbol: constructor InputStreamReader(java.net.URL)
    location: class java.io.InputStreamReader
    at readinghtml.htmlread(readinghtml.java:14)
    at readinghtml.main(readinghtml.java:39)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)






    import java.io.*;
    import java.net.*;

    /**
    *
    * @author Samvenkatram
    */
    public class readinghtml {


    public void htmlread() throws Exception
    {
    final URL url = new URL("http://www.google.com");
    final InputStream inputStream = new InputStreamReader(url);
    final BufferedReader reader= new BufferedReader(inputStream).openStream();

    String line;

    try
    {
    while ((line = reader.readLine()) != null) {
    System.out.println(line);
    }

    reader.close();


    }
    catch(Exception ex)
    {
    System.out.println(ex);
    }

    }

    public static void main(String args[]) throws Exception
    {
    readinghtml obj=new readinghtml();
    obj.htmlread();


    }


    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: i want my code to read the content of a html page... and am getting this error.

    Quote Originally Posted by samvenkatram

    final URL url = new URL("http://www.google.com");
    final InputStream inputStream = new InputStreamReader(url);
    Yeah, it's kind of difficult to get the hierarchy straight...

    Two problems with the second statement above:

    1. InputStreamReader constructor needs an InputStream argument, not a URL.

    2. Declaring an InputStream object and setting it equal to something obtained from an InputStreamReader constructor does not compute.


    Things go downhill from there:
    Quote Originally Posted by samvenkatram
    final BufferedReader reader= new BufferedReader(inputStream).openStream();
    Some problems with this
    1. The BufferedReader constructor takes an Reader as argument not an InputStream. (An InputStreamReader object would be cool here.)

    2. BufferedReader class does not have an openStream() method.

    3. The assignment statement doe not compute. (Looks to me like it is assigning a stream to a BufferedReader object. I'm not sure how it looks to a compiler.)


    One possible approach: Four statements:

    1. Declare a new URL object, url, with a String argument for the constructor. Yours is OK for this step.

    2. Declare an InputStream object and set it equal to url.openStream() (Do not use new here: You already have a URL object.)

    3. Declare a new InputStreamReader object with your InputStream object as the argument for the constructor

    4. Declare a new BufferedReader object with your inputStreamReader object as the argument for the constructor.


    You can shorten the number of lines of Java (by putting some of the new stuff in the arguments for some of the constructors), but start with something that works and make it more elegant if you feel the need for obfuscation.


    Cheers!


    Z
    Last edited by Zaphod_b; August 27th, 2012 at 01:47 PM.

Similar Threads

  1. Hoe do I read html from the web?
    By bholzer in forum Other Programming Languages
    Replies: 2
    Last Post: August 9th, 2011, 06:09 PM
  2. why a unknown symbol is visible on a jsp page which include another html pages
    By se.anilp in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 21st, 2011, 10:12 AM
  3. fetch HTML page content from the web ( by executing javascript ) using stand alone
    By johnyabu@gmail.com in forum Java Theory & Questions
    Replies: 7
    Last Post: February 8th, 2011, 07:25 AM
  4. why does this code read an html file uncontinuously
    By nasi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 25th, 2010, 09:09 PM
  5. How to read URLs from a web page
    By abitha in forum Java Theory & Questions
    Replies: 1
    Last Post: September 17th, 2009, 12:36 PM

Tags for this Thread