【问题标题】:Initials from two names plus full surname两个名字的首字母加全姓
【发布时间】:2015-10-09 17:55:19
【问题描述】:

我正在为这段代码苦苦挣扎。这个返回的是例如:“JS John Smith”但是当我尝试把两个名字加上姓氏时,我得到的只是一团糟。我希望在我输入时得到例如:“John William Smith”这样的东西:“JW Smith”,有人知道怎么做吗?

import java.io.*;
import java.io.BufferedReader;

public class ex54 {

    public static void main(String[] args) {
        System.out.print("Enter your name: ");
        BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
        String fullName = null;
        try{
            fullName = br.readLine();
        } catch (IOException ioe) {
            System.out.println("Error");
            System.exit(1);
        }
        int spacePos = fullName.indexOf(" ");
        // String firstName = fullName.substring(0, spacePos);
        // String secondName = fullName.substring(1, spacePos);
        String firstInitial = fullName.substring(0, 1);
        String secondInitial = fullName.substring(spacePos+1, spacePos+2);
        String userName = (firstInitial + secondInitial + " ").concat(fullName);

        System.out.println("Hello, your user name is: " + userName);
        }
    }
}

【问题讨论】:

  • split的好人选

标签: java names


【解决方案1】:

你可以拆分名字,假设你得到一个三名字符串:

String[] names = fullname.split(" ");
System.out.println("" + names[0].charAt(0) + names[1].charAt(0) + " " + names[2]);

【讨论】:

  • 不是通用的。如果没有给出中间名则失败,如果给出多个中间名则产生错误的结果。
【解决方案2】:
    int spacePos = -1;

    System.out.print("Hello, your user name is:");
    do {
        System.out.print(" "+fullName.substring(spacePos+1, spacePos+2));
        fullName = fullName.substring(spacePos+1);
        spacePos = fullName.indexOf(" ");
    }while(spacePos != -1);
    System.out.println("\b"+fullName);

【讨论】:

    【解决方案3】:

    只是为了好玩,这里是一个使用正则表达式的实现:

    private static String firstNamesToInitials(String name) {
        StringBuilder buf = new StringBuilder();
        Matcher m = Pattern.compile("\\b([A-Z])[A-Za-z]*\\b").matcher(name);
        String lastName = null;
        while (m.find()) {
            buf.append(m.group(1));
            lastName = m.group();
        }
        if (buf.length() <= 1)
            return lastName;
        buf.setCharAt(buf.length() - 1, ' ');
        return buf.append(lastName).toString();
    }
    

    测试

    System.out.println(firstNamesToInitials("Cher"));
    System.out.println(firstNamesToInitials("John Smith"));
    System.out.println(firstNamesToInitials("John William Smith"));
    System.out.println(firstNamesToInitials("Charles Philip Arthur George"));
    System.out.println(firstNamesToInitials("Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso"));
    

    输出

    Cher
    J Smith
    JW Smith
    CPA George
    PDFPJNRCTR Picasso
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 2019-09-22
      相关资源
      最近更新 更多