2

I've got a bit of an issue, which I've spent quite some time now trying to fix and search solutions for but to no avail. So I hope you guys can perhaps understand what I'm trying to achieve and have some suggestions?

Basically, I'm trying to write a program that will allow the user to type in a word, the program will then go to dictionary.reference.com, search the word, then retrieve the .mp3 hyperlink (or open it) and allow the user to listen to how the word is pronounced. So basically it is a word pronunciation program.

My code is still in very early stages, but my sketchy idea is that I'll have a GUI for them to enter the word, so don't mind the debugging fixed variables (such as searchWord).

Code below:

import java.net.*;
import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


public class Pronun {
     String searchWord = "development"; // Fixed variable for debugging
     Element audio; 

     public Pronun() throws Exception {
         // As program is ran, begin searching the word in the dictionary.
         URL makeSearch = new URL("http://dictionary.reference.com/browse/"+searchWord+"?s=t");
         URLConnection connectTo = makeSearch.openConnection();
         BufferedReader in = new BufferedReader(new InputStreamReader(connectTo.getInputStream()));

         String inputLine;
         while ((inputLine = in.readLine()) != null) 
             System.out.println(inputLine);
             File input = new File("output.txt");
             Document doc = Jsoup.parse(input, "UTF-8");
             Element audio = doc.select("audspk.classname").first();

             // Write the hyperlink to the file "output.txt".
             PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt", true))); 
             out.println(audio);
             out.close();           
         in.close();


    }
}

The hyperlink I'm trying to retrieve from the page, is:

<a class="audspk" href="http://static.sfdict.com/dictstatic/dictionary/audio/luna/D02/D0238600.mp3">

As you can see, I'm trying to get it through Jsoup, using it's classname - "audspk".

But basically, when I'm running the code this is the error I'm receiving:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from Elements to String

     at URLConnectionReader.main(URLConnectionReader.java:25)

I've attempted to convert it via a toString, but this also doesn't seem to work, but I must be doing something wrong there, so any help on this would be appreciated. I hope you can understand what I'm trying to achieve here.

4
  • What happens when you convert it via toString()? Commented Jul 19, 2014 at 18:09
  • I get a similar error, with not being able to convert it. Commented Jul 19, 2014 at 18:10
  • The problem appears to be in your URLConnectionReader class's main method; Could you post that code? Commented Jul 19, 2014 at 18:14
  • Can you just post the entire stack trace? I think we can find the problem from that Commented Jul 19, 2014 at 19:22

1 Answer 1

1

I have a few comments:

  1. File output.txt seems to be empty because you didn't write anything to it.
  2. If you want to print the link then use method attr of Element:

    String link = doc.select(".audspk").first().attr("href");
    
  3. jsoup can download and parse URL by himself:

    URL makeSearch = new URL("http://dictionary.reference.com/browse/" + searchWord + "?s=t");
    Document doc = Jsoup.parse(makeSearch, 1000);
    String link = doc.select(".audspk").first().attr("href");
    System.out.println(link);
    
Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct way to do it, even though I'd suggest you also use the Jsoup Connection interface as this: Document doc = Jsoup.connect(html).get(); String url = doc.select(".audspk").first().attr("href");
Forgive me for the very late reply, I have not been coding recently and was really busy. This does exactly what I need to do. I think I was over complicating it quite a bit there. Now my next aim is to save the .mp3 file from the URL it retrieves, then be able to open it locally and play the sound.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.