我在java(Netbeans)中有错误。任何建议?

时间:2016-05-09 18:02:37

标签: java netbeans

这是代码。代码可以从具有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); 
}

2 个答案:

答案 0 :(得分:1)

只需添加if语句即可检查Names的长度是否大于1并相应地调整逻辑。

答案 1 :(得分:1)

尝试这样

if (there are 2 words)
    //check both words
else
    //just check one word