First, last and sometime middle name detection Java

女生的网名这么多〃 提交于 2019-12-13 08:25:31

问题


I am trying to write a code to give off the user's name in different formats after they enter it. However, if a user does not have a middle name, the system should print that there was an error. I have it so it works perfectly if the user enters three names but does not work if the user enters two names. Here is my code:

import java.util.Scanner;

public class Assignment3 
{
  public static void main(String[] args) 
  {
    String fullName;
    Scanner in = new Scanner(System.in);
    System.out.print ("What are your first, middle, and last names? ");
    fullName = in.nextLine();
    System.out.println(fullName);
    if (fullName.contains(" "))
    {
      String[] nameParts = fullName.split(" ");
      String firstInitial = nameParts[0].substring(0,1).toUpperCase();
      String secondInitial = nameParts[1].substring(0,1).toUpperCase();
      String thirdInitial = nameParts[2].substring(0,1).toUpperCase();

      if (nameParts[2].isEmpty())
      {
        System.out.println("No Middle Name Detected");
      }
      else
      {
        System.out.println ("Your initials are: " + firstInitial + secondInitial + thirdInitial);

        String lastVariationOne = nameParts[2].substring(0, nameParts[2].length());
        lastVariationOne = lastVariationOne.toUpperCase();
        String firstVariationOne = nameParts[0].substring(0, nameParts[0].length());
        firstVariationOne = firstVariationOne.substring(0,1).toUpperCase() + firstVariationOne.substring(1, nameParts[0].length());
        System.out.println("Variation One: " + lastVariationOne + ", " + firstVariationOne + " " + secondInitial + ".");

        String lastVariationTwo = nameParts[2].substring(0, nameParts[2].length());
        lastVariationTwo = lastVariationTwo.substring(0,1).toUpperCase() + lastVariationTwo.substring(1, nameParts[2].length());
        System.out.println("Variation Two: " + lastVariationTwo + ", " + firstVariationOne);
      }
    }
    else
    {
      System.out.println("Wrong. Please enter your name properly.");
    }
  }
}

回答1:


Instead of this:

if (nameParts[2].isEmpty())
{
    System.out.println("No Middle Name Detected");
}

something like

if(nameParts.length != 3)
{
    System.out.println("Invalid entry");
}

might be preferrable.

Basically, in the case that there are only two names entered, split() will return an array of length 2, whose elements are accessible by indices 0 and 1.

But in your if condition you attempt to access index 2, which could be out of bounds (it would be OOB for the case where you entered only two names).

To resolve this, you could either (a) try it like you do, but catch the ArrayIndexOutOfBoundsException or (b) check first that split produced a properly sized array, then go from there (this was the approach I took with the change I listed).

I'd suggest (b), but both approaches seem fine.




回答2:


If you don't input middlename, would the array size be 2? So there is NO namespart[2]. Just check size of namespart. @jedwards jedwards's solution is there.



来源:https://stackoverflow.com/questions/18883130/first-last-and-sometime-middle-name-detection-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!