First of all, my target here is to read from a URL, parse it, and take information from it and give it to another method sendMessage which then sends it to an IRC client. I haven’t been doing java long so I’m adapting things I code I find on the internet. The below methods work when they are declared in their own class file, and run by passing the URL to the main method, but I can’t seem to get it to work either by calling the class from MyBot or declaring the methods in my MyBot file.
public static BufferedReader read(String url) throws Exception{
return new BufferedReader(
new InputStreamReader(
new URL(url).openStream()));}
public static String WebURL (String[] args) throws Exception{
BufferedReader reader = read(args[0]);
String line = reader.readLine();
String newURL = new String();
while (line != null) {
if (line.contains("<meta ")) {
String predef = new String(line.split("<meta content='")[1]);
//line.indexOf finds where to take up till, substring takes from beginning until there
String def = new String(predef.substring(0, (predef.indexOf("' name="))));
newURL = def;
}
else {
newURL = "No definition available";
}
line = reader.readLine(); }
return newURL;
}
Now in the main I have the following:
if (message.startsWith("!def ")) {
String[] myArray = new String[2];
myArray[0] = message;
MyBot myURL = new MyBot();
myURL.WebURL(myArray);
sendMessage(channel, myURL);
}
The error I get when trying to compile is the following:
MyBot.java:62: cannot find symbol
symbol : sendMessage(java.lang.String,java.lang.String)
cannot be applied to (java.lang.String,MyBot)
sendMessage(channel,myURL);
^
So it seems to be saying that MyURL isn't a String, but the method return type is String.. so I'm obviously something here (probably I'm using return wrong?)
Very grateful of anyone's help, and if there is a better way than this of doing what I want I would be happy to hear that too 🙂 Thanks!
EDIT: To the first few answers whichtell me to pass myURL.WebURL(myArray) to sendMessage, I already this but got the following error which confused me more:
unreported exception java.lang.Exception; must be caught or declared to be throw
I understand this is because WebURL throws an exception but I don't know how to declare/catch this when I'm declaring a String..
myUrl is a type MyBot from the line:
If you change the next lines from:
to:
it should work
EDIT: If you’re throwing an exception from myURL.WebUrl(myArray), enclose it in a try/catch block: