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:
MyBot myURL = new MyBot();
If you change the next lines from:
myURL.WebURL(myArray);
sendMessage(channel, myURL);
to:
String actualUrl = myURL.WebURL(myArray);
sendMessage(channel, actualUrl);
it should work
EDIT: If you're throwing an exception from myURL.WebUrl(myArray), enclose it in a try/catch block:
String actualUrl = null;
try {
actualUrl = myURL.WebURL(myArray);
} catch (Exception e) {
actualUrl = "Something default";
}
You need to pass the return value of myURL.WebURL(myArray)
to send messgae
String myURLString = myURL.WebURL(myArray);
sendMessage(channel, myURLString);
The error message is quite correct - they're usually quite helpful and are almost always very clear about type errors.
Well myURL
is not a string it is an MyBot
. You need to assgin the result of calling myURL.WebURL(myArray);
to a String
variable and use that to call sendMessage();
:
if (message.startsWith("!def ")) {
String[] myArray = new String[2];
myArray[0] = message;
MyBot myURL = new MyBot();
String myURLString = myURL.WebURL(myArray);
sendMessage(channel, myURLString);
}
Try changing from
myURL.WebURL(myArray);
sendMessage(channel, myURL);
to this:
sendMessage(channel, myURL.WebURL(myArray));
Your WebURL method returns a String value, but you aren't doing anything with it. Instead, you're passing the myURL object itself, which is why the compiler is telling you the "cannot be applied to (java.lang.String,MyBot)" message. It's saying that you're passing something of type MyBot to something that requires a String.
精彩评论