这是代码。代码可以从具有2个名字的人中提取第一个字母,但由于我使用分割,因此无法从具有1个名字的人中提取第一个字符。
String name = jTextField1.getText().toUpperCase() + "";
String Surname = jTextField2.getText().toUpperCase().toString();
String Names[] = new String[1];
Names = name.split(" ");
int x = Names[1].length();
String initials = "";
if(x>0) {
initials = (Surname)+" "+(Names[0].charAt(0)+"") +(Names[1].charAt(0)+"");
jTextArea1.append("Wakefileds property "+"\n"+initials);
} else {
initials = (Surname)+ " " + (Names[0].charAt(0) + "");
jTextArea1.append("Wakefileds property "+"\n"+initials);
}
答案 0 :(得分:1)
只需添加if
语句即可检查Names
的长度是否大于1并相应地调整逻辑。
答案 1 :(得分:1)
尝试这样
if (there are 2 words)
//check both words
else
//just check one word