简体   繁体   English

方法返回类型String不是字符串

[英]Method return type String is not a String

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. 首先,我的目标是从URL读取,解析并从中获取信息,并将其提供给另一个方法sendMessage ,然后将其发送给IRC客户端。 I haven't been doing java long so I'm adapting things I code I find on the internet. 我没有做Java很久了,所以我正在适应我在网上找到的代码。 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. 下面的方法在它们自己的类文件中声明时起作用,并通过将URL传递给main方法来运行,但是我似乎无法通过从MyBot调用类或在MyBot声明这些方法来MyBot文件。

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?) 因此,似乎要说MyURL不是字符串,但是方法的返回类型是String ..因此,我在这里很明显(可能我在使用return错误?)

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: 编辑:对于告诉我将myURL.WebURL(myArray)传递给sendMessage的前几个答案,我已经这样做了,但遇到了以下错误,这使我更加困惑:

unreported exception java.lang.Exception; must be caught or declared to be throw 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.. unreported exception java.lang.Exception; must be caught or declared to be throw我理解这是因为WebURL抛出异常,但是在声明String时我不知道如何声明/捕获此异常。

myUrl is a type MyBot from the line: myUrl是该行中的MyBot类型:

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: 编辑:如果要从myURL.WebUrl(myArray)引发异常,请将其包含在try / catch块中:

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 您需要传递myURL.WebURL(myArray)的返回值以发送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 . 好吧, myURL不是字符串,而是MyBot You need to assgin the result of calling myURL.WebURL(myArray); 您需要确定调用myURL.WebURL(myArray);的结果myURL.WebURL(myArray); to a String variable and use that to call sendMessage(); 到一个String变量,并使用它来调用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. 您的WebURL方法返回一个String值,但您并未对其进行任何操作。 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. 相反,您要传递myURL对象本身,这就是为什么编译器告诉您“无法应用于(java.lang.String,MyBot)”消息的原因。 It's saying that you're passing something of type MyBot to something that requires a String. 就是说,您要将MyBot类型的东西传递给需要String的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM