Java 方法没有返回字符串?

Java 方法没有返回字符串?,java,eclipse,string,return,Java,Eclipse,String,Return,为Java解决一些初学者问题: 给定两个字符串,将它们附加在一起,称为连接并返回结果 但是,如果串联创建了一个双字符,则省略其中一个字符,这样abc和cat就会生成abcat 我的代码: public static String conCat(String a, String b) { //abc (c) == cat (c) if (a.substring(a.length()-1,a.length()) == b.substring(0,1)) {

为Java解决一些初学者问题:

给定两个字符串,将它们附加在一起,称为连接并返回结果

但是,如果串联创建了一个双字符,则省略其中一个字符,这样abc和cat就会生成abcat

我的代码:

public static String conCat(String a, String b) {
    //abc (c) == cat (c)
    if (a.substring(a.length()-1,a.length()) == b.substring(0,1)) {
          
        //return (ab) + (cat)
        return a.substring(0,a.length()-2) + b;
         
    //cat (c) == abc (c)
    } else if(b.substring(0,1) == a.substring(a.length()-1,a.length())) {
              
        //return (abc) + (at)
        return a + b.substring(2,b.length());
              
    } else if (a.equals("") || b.equals("")) {
        return a + b;
    }
}

我不明白为什么Eclipse不能识别返回的字符串。

实际上,它可以。但是您所有的返回语句都取决于一个条件,因此在某些情况下您没有提供返回语句

在这些情况下,该方法不会返回任何内容,即使它应该返回字符串

加:


请在方法的末尾重试。

首先,您要将字符串与==进行比较,后者通过引用对它们进行比较。这意味着相等的字符串可能不会返回true。为避免此问题,请始终使用.equals来比较字符串

其次,请记住,if语句是按照指定的顺序检查的。因为您想首先检查空字符串,所以应该将该字符串放在顶部

第三,必须在所有代码路径上返回一些内容。如果所有If语句都为false,则不会返回任何内容。如果您添加了else,则返回a+b;你应该得到想要的行为

此外,我建议使用稍微不同的方法:

public static String conCat(String a, String b) {
    //If either String is empty, we want to return the other.
    if (a.isEmpty()) return b;
    else if (b.isEmpty()) return a;
    else {
        //If the last character of a is the same as the first character of b:
        //Since chars are primitives, you can (only) compare them with ==
        if (a.charAt(a.length()-1) == b.charAt(0))
            return a + b.subString(1);
        //Otherwise, just concatenate them.
        else
            return a + b;
    }
}
请注意,您可以省略else块,因为return将在那里结束方法的执行,因此这也将起作用:

public static String conCat(String a, String b) {
    if (a.isEmpty()) return b;
    if (b.isEmpty()) return a;
    if (a.charAt(a.length()-1) == b.charAt(0)) return a + b.subString(1);
    return a + b;
}

如果所有if条件都为false,即执行到达方法末尾,您希望返回什么?另外,不要使用==来比较字符串-请参阅风格和有争议的注释:我不喜欢堆叠if/return/else。我发现没有其他的更清楚。如果我们仍然返回,则不需要其他条件。如果所有If条件都为false,即执行到达方法末尾,您希望返回什么?我不是一个聪明人。谢谢你,乔恩。第二个if似乎毫无意义。回答得很好,比原始代码可读性好得多+如果删除其中的所有其他项,则返回a+b子字符串1;你不必瞎摆弄a的长度。它也应该是a.charAta.length-1
public static String conCat(String a, String b) {
    //If either String is empty, we want to return the other.
    if (a.isEmpty()) return b;
    else if (b.isEmpty()) return a;
    else {
        //If the last character of a is the same as the first character of b:
        //Since chars are primitives, you can (only) compare them with ==
        if (a.charAt(a.length()-1) == b.charAt(0))
            return a + b.subString(1);
        //Otherwise, just concatenate them.
        else
            return a + b;
    }
}
public static String conCat(String a, String b) {
    if (a.isEmpty()) return b;
    if (b.isEmpty()) return a;
    if (a.charAt(a.length()-1) == b.charAt(0)) return a + b.subString(1);
    return a + b;
}