我希望打印姓氏,首字母只有大写字母,但我得到所有姓氏大写。我希望用户输入他们的名字,如乔丹托雷斯,并回馈托雷斯,J。
import java.util.*; // for Scanner
public class ProcessName { public static void main(String[] args) { String name;
Scanner console = new Scanner(System.in);
System.out.print("Type your name: "); //send to user
name = console.nextLine();
String[] nameArray = name.split(" ");//split string into array
String firstName = name.valueOf(name.charAt(0)).toUpperCase(); //get first letter
String lastName = name.substring(name.indexOf(" ") + 1).toUpperCase();
String myName = lastName + ", " + firstName;//give last and first name
System.out.println("Your name is: " + myName + ".");
我的输出是TORRES,J。
答案 0 :(得分:2)
// Get the last name as entered by the user
String lastName = name.substring(name.indexOf(" ") + 1);
// Upcase the first character, then append the remaining characters
lastName = Character.toUpperCase(lastName.charAt(0)) + lastName.substring(1);