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 4 of 4

Thread: whats up with this jsoup code

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default whats up with this jsoup code

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package javaapplication15;

    import java.io.IOException;
    import javax.lang.model.element.Element;
    import javax.lang.model.util.Elements;
    import javax.swing.text.Document;
    import org.jsoup.Jsoup;


    public class JavaApplication15 {
    public static void main(String[] args) {

    Document doc;
    try {

    // need http protocol

    doc = Jsoup.connect("http://google.com").get();//this line generates error:
    required: javax.swing.text.Document
    found: org.jsoup.nodes.Document

    // get page title
    String title = doc.title();
    System.out.println("title : " + title);

    // get all links
    Elements links = doc.select("a[href]");
    for (Element link : links) {

    // get the value from href attribute
    System.out.println("\nlink : " + link.attr("href"));
    System.out.println("text : " + link.text());

    }

    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    i have jsoup imported cant fiqure out whats wrong as i took this codde from jsoup website thanks.

    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: whats up with this jsoup code

    doc = Jsoup.connect("http://google.com").get();//this line generates error:
    required: javax.swing.text.Document
    found: org.jsoup.nodes.Document
    The compiler message is saying that you declared doc as an instance of javax.swing.text.Document (that's the left hand side of the assignment) but you provided it an instance of org.jsoup.nodes.Document (returned by get() on the right hand side.) You can always tell what sort of thing you are dealing with by looking up the API documentation. That documentation shows connect() returning a Document and, if you click on the link in the docs you are taken to that of org.jsoup.nodes.Document.

    The solution is to remove the "import javax.swing.text.Document;" line and replace it by importing the class you are going to use.

    The same problem may occur with Element(s) where you have imported some very out of the way classes.

    It is common for IDEs to auto import classes. But they usually ask which class you mean to use (and, if not, use a different IDE). It is important that you answer correctly. To begin with you might find this hard: I mean if you are trying things out, you may not be completely sure what the code is doing! Your only defense here is to have that API documentation open and consult it to see what each method call of the code is doing. And if you are given a choice between a Jsoup class and a standard one of the same name, the Jsoup one might be a better guess.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    bean (April 6th, 2013)

  4. #3
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: whats up with this jsoup code

    thanks never noticed that before i allways just got what i wanted from fix imports and hitting return thanks fixed the prob and learned something very important

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: whats up with this jsoup code

    You're welcome.

    (I haven't added anything to your other thread about getting the contents of a web page in .doc format, not because I'm ignoring what you asked, but just because I don't have anything useful to say beyond pointing out how difficult a task it is. Jsoup looks like it will perform the more modest task of getting the text from a url.)

Similar Threads

  1. import, .java files, and jsoup-1.6.1
    By dalydir in forum Java Theory & Questions
    Replies: 2
    Last Post: January 20th, 2012, 07:49 PM
  2. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  3. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  4. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM