0

编译这个程序时,我收到“reformatName”方法的错误,因为它“必须返回 java.lang.String 类型的结果”我假设这是它已经返回的内容!该方法采用的每条路径,最终都会返回一个字符串。(对不起,如果这是可怕的格式/编写;这是我第一次在这里发帖。)

import java.util.*;
public class NameFormatChallenge {
    public static void main(String[] args) {
    Scanner wordInput = new Scanner(System.in);
    System.out.println("Enter a name");
    String userInput = wordInput.nextLine();
    String[] name = userInput.split(" ");
    System.out.println(reformatName(name));
}

public static String reformatName(String[] name) {
    if(name[1].charAt(1)=='.')
      return formatOne(name);
    else if(name[1].length()==1)
      return formatTwo(name);
    else if(name[0].charAt(name[0].length()-1)!=',')
      return formatThree(name);
    else if(name[2].length()>2)
      return formatFour(name);
    else if(name[2].charAt(name[2].length()-1)=='.')
      return formatFive(name);
    else if(name[2].length()==1)
      return formatSix(name);
}

public static String formatOne(String[] name) {
    name[1] = name[1].substring(0,1);
    String tempZero = name[0];
    String tempOne = name[1];
    String tempTwo = name[2];
    name[0] = tempTwo;
    name[1] = tempZero;
    name[2] = tempOne;
    return nameConcatenation(name);
}

public static String formatTwo(String[] name) {
    String tempZero = name[0];
    String tempOne = name[1];
    String tempTwo = name[2];
    name[0] = tempTwo;
    name[1] = tempZero;
    name[2] = tempOne;
    return nameConcatenation(name);
}

public static String formatThree(String[] name) {
    String tempZero = name[0];
    String tempOne = name[1];
    String tempTwo = name[2];
    name[0] = tempTwo;
    name[1] = tempZero;
    name[2] = tempOne;
    return nameConcatenation(name);
}

public static String formatFour(String[] name) {
    String tempOne = name[1];
    String tempTwo = name[2];
    name[1] = tempTwo;
    name[2] = tempOne;
    return nameConcatenation(name);
}

public static String formatFive(String[] name) {
    name[2] = name[2].substring(0,1);
    String tempOne = name[1];
    String tempTwo = name[2];
    name[1] = tempTwo;
    name[2] = tempOne;
    return nameConcatenation(name);
}

public static String formatSix(String[] name) {
    String tempOne = name[1];
    String tempTwo = name[2];
    name[1] = tempTwo;
    name[2] = tempOne;
    return nameConcatenation(name);
}

public static String nameConcatenation(String[] name) {
    StringBuilder b = new StringBuilder();
    int endOfArrZero = name[0].length()-1;
    int endOfArrOne = name[1].length();
    int endOfArrTwo = name[2].length()+1;
    for (int i = 0; i<3; i++) {
      b.append(String.valueOf(name[i]));
      if(i!=2) {
        b.append(" ");
      }
    }
    if(b.charAt(endOfArrZero) != ',') {
      b.insert(endOfArrZero,",");
      endOfArrOne=endOfArrOne+1;
      endOfArrTwo=endOfArrTwo+1;
    }
    if(b.charAt(endOfArrOne) == '.') {
      b.deleteCharAt(endOfArrOne);
      endOfArrTwo=endOfArrTwo-1;
    }
    String Finalname = b.toString();
    return Finalname;
}
4

4 回答 4

2

如果没有条件评估为真,该reformatName方法将不会返回字符串。这段代码的执行将通过每个条件到达方法的结尾,该方法没有返回语句。通过添加return "";我尊重方法的签名,但您可能应该抛出异常或返回名称。

public static String reformatName(String[] name) {
  if(name[1].charAt(1)=='.')
      return formatOne(name);
  else
    if(name[1].length()==1)
      return formatTwo(name);
  else
    if(name[0].charAt(name[0].length()-1)!=',')
      return formatThree(name);
  else
    if(name[2].length()>2)
      return formatFour(name);
  else
    if(name[2].charAt(name[2].length()-1)=='.')
      return formatFive(name);
  else
    if(name[2].length()==1)
      return formatSix(name);

  return "";
  /* this will fix it but I'm not sure what
     you should return when no conditions evaluate to true.*/
}
于 2013-05-10T00:41:34.870 回答
0

如果您if的 s 都不匹配,则该函数将不会返回任何内容。

编译器无法处理。

于 2013-05-10T00:42:02.107 回答
0

如果它不满足所有条件,则不返回任何内容。那就是问题所在。

于 2013-05-10T00:42:06.437 回答
0

您应该从 if 循环中返回字符串。因为 return 语句仅在某些条件下发生,如果没有发生该条件,则没有返回方法。

于 2013-05-10T00:43:39.910 回答