1

当有人只输入他们的两个名字时,我试图让代码输出,但我无法弄清楚。我尝试过使用 if (nameFML==null) and (nameFML[2].isEmpty()),但每当我输入像 John Doe 这样的名称时,我仍然会收到“线程主 java.lang.arrayindexoutofboundsexception:2 中的异常”错误。除此之外,该程序会做它应该做的事情。

    import java.util.Scanner;
    public class Assignment3
    {
      public static void main(String[] args)
      {

       //Declaring variables
        String inputName;
        Scanner in = new Scanner(System.in);

       //Asking user for keyboard input.
        System.out.println("What are your first, middle, and last names? ");
        inputName = in.nextLine();

       //Users First Input restated
        System.out.println(inputName);

       //Splitting the string "inputName" up by making spaces(" ") delimiters.
       if (inputName.contains(" "))
        {
          String[] nameFML = inputName.split(" ");

       // Creates new strings from the new "nameFML" variable, which was created from "inputName" being split.
          String firstInitial = nameFML[0].substring(0,1).toUpperCase();
          String middleInitial = nameFML[1].substring(0,1).toUpperCase();
          String lastInitial = nameFML[2].substring(0,1).toUpperCase();

       //The if method determines whether or not the user inputed in three tokens, two, or an incorrect amount.
          if (nameFML.length== 2)
          {
            System.out.println("Your initials are: " + firstInitial + middleInitial);

        //Separated the print the Variation One print command because it's the only way I could get ".toUpperCase" to work properly.
            System.out.print("Variation One: " + (nameFML[1].toUpperCase()));
            System.out.println(", " + nameFML[0]);

            System.out.println("Variation Two: " + nameFML[1] + ", " + nameFML[0]);


          }
          else
          {
            System.out.println("Your initials are: " + firstInitial + middleInitial + lastInitial);

        //Separated the print the Variation One print command because it's the only way I could get ".toUpperCase" to work properly.
            System.out.print("Variation One: " + (nameFML[2].toUpperCase()));
            System.out.println( ", " + nameFML[0] + " " + lastInitial + ".");

            System.out.println("Variation Two: " + nameFML[2] + ", " + nameFML[0] + " " + nameFML[1]);
          }
        }
        else
        {
      System.out.println("Wrong. Please enter your name properly.");
    }
  }
}
4

3 回答 3

1

任务是找到首字母。为此,您不一定需要知道名称,甚至不需要知道每个首字母是什么。

您可以在一行代码中找到首字母缩写:

String initials = input.replaceAll("(?<\\b\\w)\\w*\\s*", "");

这会将每个单词的第一个字母和任何尾随空格替换为空白(有效及时删除它们)。

这段代码的好处是它适用于任意数量的名称,包括只有一个名称甚至没有名称(即空白输入),并且重要的是没有错误。

于 2013-09-21T03:57:44.267 回答
1

问题是如果字符串只有两部分,则数组的第三个元素(索引2)不是空的——它只是不存在。因此,代码在

String lastInitial = nameFML[2].substring(0,1).toUpperCase();

如果你移动这些行

String firstInitial = nameFML[0].substring(0,1).toUpperCase();
String middleInitial = nameFML[1].substring(0,1).toUpperCase();
String lastInitial = nameFML[2].substring(0,1).toUpperCase();

进入适当的 if 语句,一切都应该正常。nameFML.length是正确的检查。

以供将来参考,exception in thread main java.lang.arrayindexoutofboundsexception: 2这意味着您尝试访问2少于 3 个元素的数组的索引(当您这样做时发生nameFML[2])。

于 2013-09-21T03:14:51.413 回答
0

代替上面的 if-else,使用 for 循环

String firstInitial = "";
String middleInitial = "";
String lastInitial = "";
String[] nameFML = inputName.split(" ");
for(int i=o; i<nameFML.length; i++)
{
if(i==0)
 firstInitial = nameFML[0].substring(0,1).toUpperCase();
else if(i=1)
 middleInitial = nameFML[1].substring(0,1).toUpperCase();
else if(i=2)
 lastInitial = nameFML[2].substring(0,1).toUpperCase();
}


System.out.println("Your initials are: " + firstInitial + middleInitial + lastInitial);
于 2013-09-21T03:22:23.860 回答