2015-05-28 49 views
0

如果有人輸入!音樂,我正在寫一個應該從此網頁抓取藝術家姓名的IRC機器人http://whatthefuckshouldilistentorightnow.com/artist.php?artist=e&x=36&y=30。這是代碼的一部分:Jsoup符號錯誤

public void onMessage(String channel, String sender, String login, String hostname, String message) { 

if (messageIC.startsWith("!music ")) { 
     String musicy = "id=\"artist\">" 
     try { 
      Document doc = Jsoup.connect("http://whatthefuckshouldilistentorightnow.com/artist.php?artist=e&x=36&y=30").get(); 
     } 
     catch (IOException e){ 
     } 
     String texty = doc.body().text(); // "An example link" 
     if (texty.contains(musicy)) { 
      String artisty = texty.substring(musicy); 
      int artsy1 = artisty.indexOf(">") + 1; 
      int artsy2 = artisty.indexOf("</div>"); 
      artisty = artisty.substring(artsy1, artsy2); 
      sendMessage(channel, "artisty: " + artisty); // */ 
     }      
     else { 
      sendMessage(channel, "Something went wrong."); 
     } 
} 

} 

但是,我收到String texty = doc.body()。text();.該消息是:

「找不到符號

符號:方法體()」

任何想法,什麼是錯的還是如何提高代碼將不勝感激。

回答

0

這是因爲你在try catch中聲明你的var。你應該在try-catch中使用它,或者在之前聲明它。試試這個例子...

Document doc = null; 
try { 
     doc = Jsoup.connect("http://whatthefuckshouldilistentorightnow.com/artist.php?artist=e&x=36&y=30").get(); 
} catch (IOException e){} 
// rest of code 
+0

我試過了,我仍然在doc.body()。text()接收到一個錯誤。 – quibblify

+0

您是否嘗試過符合條件的'Document'來確保您引用的是JSoup類而不是另一個'Document'類? (也可以像@Amir Raminfar所說的那樣從'try'子句中取出聲明) – theduck