首先,最后和有时中间名检测Java [英] First, last and sometime middle name detection Java

查看:51
本文介绍了首先,最后和有时中间名检测Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写代码,以在用户输入用户名后以不同的格式发布用户名.但是,如果用户没有中间名,则系统应打印出错误信息.我有它,因此如果用户输入三个名称,它会完美工作,但如果用户输入两个名称,则它不工作.这是我的代码:

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.");
    }
  }
}

推荐答案

代替此:

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

类似

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

可能更可取.

基本上,在仅输入两个名称的情况下,split()将返回一个长度为2的数组,其元素可通过索引0和1进行访问.

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.

但是在您的if条件下,您尝试访问索引2,该索引可能超出范围(对于您仅输入两个名称的情况, 将是OOB).

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).

要解决此问题,您可以(a)像尝试一样尝试,但要捕获ArrayIndexOutOfBoundsException或(b)首先检查split生成了适当大小的数组,然后从那里进行检查(这是我对更改我列出的内容.)

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).

我建议(b),但这两种方法似乎都很好.

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

这篇关于首先,最后和有时中间名检测Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆