0

我正在编写一个程序来打印以下内容

用户输入名称,如下所示 --> first middle last 打印:
FML
变体一:LAST,First M.
变体二:Last,First Middle

现在,我需要一个 if 语句,这样如果只输入一个名字,它就会显示“错误,输入不正确”

我编写了这个可怕且非常规的代码,但是嘿,这是我以前编写的第一件事,所以我想我们都从某个地方开始。

import java.util.Scanner;

public class name {
    public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    String fullName = input.nextLine();
    String firstName;
    String middleName;
    String lastName;

    //Declares length of entire name
    int nameLength = fullName.length();

    //Declares int where first space is
    int a = fullName.indexOf(" ");

    //Declares int where second space is
    int b = fullName.lastIndexOf(" ");

    //If they equal each other, then there is only one space
    if ( a == b )
    {
        firstName = fullName.substring(0,a);

        lastName = fullName.substring(a+1,nameLength);

        String firstNameInitial = firstName.substring(0,1);
        String lastNameInitial = lastName.substring(0,1);
        String upperCaseInitials = (firstNameInitial.toUpperCase() + lastNameInitial.toUpperCase());

        firstName = fullName.substring(0,a);

        lastName = fullName.substring(b+1,nameLength);

        System.out.println("Your initials are: " + upperCaseInitials);
        System.out.println("Variation One: " + lastName.toUpperCase() + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a));
        System.out.println("Variation Two: " + lastNameInitial.toUpperCase() + lastName.substring(1,lastName.length()) + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a));

    }

    //If a < b then it will notice a middle name exists due to multiple spaces
    else if ( a < b )
    {
        firstName = fullName.substring(0,a);

        middleName = fullName.substring(a+1,b);

        lastName = fullName.substring(b+1,nameLength);

        String firstNameInitial = firstName.substring(0,1);
        String middleNameInitial = middleName.substring(0,1);
        String lastNameInitial = lastName.substring(0,1);
        String upperCaseInitials = (firstNameInitial.toUpperCase() + middleNameInitial.toUpperCase() + lastNameInitial.toUpperCase());

        //MNIC = Middle Name Initial Capitalized
        String MNIC = middleNameInitial.toUpperCase();

        //MNIMFC = Middle Name Initial Minus First Character
        String MNIMFC = middleName.substring(1, middleName.length());

        System.out.println("Your initials are: " + upperCaseInitials);
        System.out.println("Variation One: " + lastName.toUpperCase() + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) + " " + middleNameInitial.toUpperCase() + "." );
        System.out.println("Variation Two: " + lastNameInitial.toUpperCase() + lastName.substring(1,lastName.length()) + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) +  " " + MNIC  + MNIMFC);


    }







}

}

4

5 回答 5

1

您可以使用该String.split()函数将字符串沿分隔符拆分为各个部分。

在你的情况下,这将是空间 ( " ")

尝试:

Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
String firstName;
String middleName;
String lastName;

String[] parts = fullName.split(" ");

if(parts.length() == 3){
    // 3 words were entered, so there is a middle name
}
// ...
于 2013-09-19T07:52:47.213 回答
0

您可以添加此检查

if(fullName.indexOf(" ")==-1 || (fullName.indexOf(" ") == fullName.lastIndexOf(" "))){
    // The first check is to check if only firstname was given and the second check is to check if only first and middle names were given.
    // If first + middle is a valid scenario, you can remove the second half of the if condition
    System.out.println("error, incorrect input");
    System.exit(0);
}

在您的代码中的以下语句之前。

int nameLength = fullName.length();
于 2013-09-19T07:52:54.243 回答
0

您可以简单地检查a == -1. indexOf如果未找到(根据文档)返回 -1。

if (a == -1)
   System.out.println("Error, invalid input!");
else if (a == b)
...
于 2013-09-19T07:53:06.973 回答
0

您可以按照以下步骤缩小到您所说的条件:

  1. fullName在语句之前修剪输入字符串int a = fullName.indexOf(" ");
  2. 接下来检查空格的索引(即ab变量的值)是否为-1,那么你可以假设输入只包含一个单词,大概是Firstname
  3. 打印错误信息“错误,输入错误”
于 2013-09-19T07:59:39.723 回答
0

因为这是你的第一次尝试,我会给你一个修改后的代码版本:

public class name {
    public static void main(String[]args)
    {
        Scanner input = new Scanner(System.in);
        String fullName = input.nextLine();
        String firstName;
        String middleName;
        String lastName;

        //Declares length of entire name
        int nameLength = fullName.length();

        //Declares int where first space is
        int a = fullName.indexOf(" ");

        //Declares int where second space is
        int b = fullName.lastIndexOf(" ");

        /*** Use the split function to split the names with spaces as delimiter **/
        String[] n = fullName.split(' ');

        firstName = n[0];

        if( n.length == 2 ) {
            lastName = n[1];
        }

        if( n.length > 3 ) {
            lastName = n[1];
            middleName = n[2];
        }

        String firstNameInitial = firstName.substring(0,1);
        String middleNameInitial = middleName.substring(0,1);
        String lastNameInitial = lastName.substring(0,1);
        String upperCaseInitials = (firstNameInitial.toUpperCase() + middleNameInitial.toUpperCase() + lastNameInitial.toUpperCase());

        //MNIC = Middle Name Initial Capitalized
        String MNIC = middleNameInitial.toUpperCase();

        //MNIMFC = Middle Name Initial Minus First Character
        String MNIMFC = middleName.substring(1, middleName.length());

        System.out.println("Your initials are: " + upperCaseInitials);
        System.out.println("Variation One: " + lastName.toUpperCase() + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) + " " + middleNameInitial.toUpperCase() + "." );
        System.out.println("Variation Two: " + lastNameInitial.toUpperCase() + lastName.substring(1,lastName.length()) + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) +  " " + MNIC  + MNIMFC);
    }
}
于 2013-09-19T08:08:00.480 回答