gpt4 book ai didi

Java字符串返回方法不返回字符串?

转载 作者:行者123 更新时间:2023-12-02 07:02:24 24 4
gpt4 key购买 nike

编译此程序时,我收到“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;
}

最佳答案

如果没有任何条件计算为 true,则 reformatName 方法将不会返回字符串。该代码的执行将通过每个条件直到方法结束,该方法没有 return 语句。通过添加 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.*/
}

关于Java字符串返回方法不返回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16473412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com