Java String Return Method Not Returning String?

javastring

When compiling this program, I get an error for the "reformatName" method, as it "must return a result of type java.lang.String" I was under the assumption this is what it already returns! Each path the method takes, it eventually returns a string.
(Sorry if this is horribly formatted/written; it's my 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 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;
}

Best Solution

The reformatName method will not return a String if none of the conditionals evaluate to true. The execution of this code will fall through each of the conditionals to end of the mehthod, which has no return statement. By adding return ""; I honor the signature of the method but you should probably throw an exception or return the 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 "";
  /* this will fix it but I'm not sure what
     you should return when no conditions evaluate to true.*/
}