Java String Return Method Not Returning String? -
this question has answer here:
when compiling program, error "reformatname" method, "must return result of type java.lang.string" under assumption returns! each path method takes, returns string. (sorry if horribly formatted/written; it's first time posting here.)
import java.util.*; public class nameformatchallenge { public static void main(string[] args) { scanner wordinput = new scanner(system.in); system.out.println("enter 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; (int = 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; }
the reformatname
method not return string if none of conditionals evaluate true. execution of code fall through each of conditionals end of mehthod, has no return statement. adding return "";
honor signature of method should throw exception or return 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); return ""; /* fix i'm not sure should return when no conditions evaluate true.*/ }
Comments
Post a Comment