错误是字符串索引超出范围:0。老实说,我什至不知道我所做的是否正确。
我必须创建一个 wholeName 方法和一个初始方法。他们的名字不言自明,几乎整个名字都会根据用户给我的内容打印出名字。如果他们只给了我一个名字和姓氏,那我就回来了。
缩写是相同的位。它从名称输入中获取第一个字母,并且只返回首字母。就像约翰·李·史密斯会是 JLS
谁能帮我理解什么是错的?如果你不能告诉我只是一个初学者。
我有一个测试程序,它返回错误 java.lang.StringIndexoutofboundexception: String index out of Range: 0 at java.lang.String.charAt(String.java:658) at PersonName.Initials(PersonName.java:131) atTestPersonName2。 testOneName(TestPersonName2.java:50)
import java.util.StringTokenizer;
public class PersonName
{
private int numberOfNames ;
private String firstName;
private String middleName;
private String lastName;
private String suffixName;
PersonName(){
firstName="";
middleName="";
lastName="";
suffixName="";
numberOfNames=0;
}
public String getFirstName(){
return firstName;
}
public String getMiddleName(){
return middleName;
}
public String getLastName(){
return lastName;
}
public String getSuffixName(){
return suffixName;
}
public void setFirstName(String f){
if((f.length() == 0) && (firstName.length() == 0)){
numberOfNames = numberOfNames;
}else if(( f.length() > 0) && (firstName.length() == 0)){
numberOfNames++;
}
firstName = f;
}
public void setMiddleName(String m){
if((m.length() == 0) && (middleName.length() == 0)){
numberOfNames = numberOfNames;
}else if(( m.length() > 0) && (middleName.length() == 0)){
numberOfNames++;
}
middleName = m;
}
public void setLastName(String l){
if((l.length() == 0) && (lastName.length() == 0)){
numberOfNames = numberOfNames;
}else if(( l.length() > 0) && (lastName.length() == 0)){
numberOfNames++;
}
lastName = l;
}
public void setSuffixName(String s){
if((s.length() == 0) && (suffixName.length() == 0)){
numberOfNames = numberOfNames;
}else if(( s.length() > 0) && (suffixName.length() == 0)){
numberOfNames++;
}
suffixName = s;
}
public int getNumberOfNames(){
return numberOfNames;
}
PersonName(String wholename)
{ firstName=""; middleName=""; lastName=""; suffixName=""; numberOfNames=0;
// store the name passed in to the method
final int MAXNAMES = 4;
String [] words = new String [MAXNAMES];
String testname=wholename;
// initialize number of names found
int numnames=0;
// on each iteration remove one name
StringTokenizer st = new StringTokenizer(wholename);
while (st.hasMoreTokens()) {
words[numnames++]=st.nextToken();
}
if (numnames ==1)
{ setFirstName(words[0]);}
else if (numnames == 2)
{ setFirstName(words[0]);
setLastName(words[1]);
}
else if (numnames == 3)
{ setFirstName(words[0]);
setMiddleName(words[1]);
setLastName(words[2]);
}
else if (numnames == 4)
{ setFirstName(words[0]);
setMiddleName(words[1]);
setLastName(words[2]);
setSuffixName(words[3]);
}
}
public String EntireName(){
String EntireName = " ";
//String EntireName = getFirstName() + " " + getMiddleName() + " " + getLastName() + " " + getSuffixName();
if(getNumberOfNames() == 0){
EntireName = " ";
}else if(getNumberOfNames() == 1){
EntireName = getFirstName();
}else if(getNumberOfNames() == 2){
EntireName = getFirstName() + " " + getMiddleName();
}else if(getNumberOfNames() == 3){
EntireName = getFirstName() + " " + getMiddleName() + " " + getLastName();
}else {
EntireName = getFirstName() + " " + getMiddleName() + " " + getLastName() + " " + getSuffixName();
}
return EntireName;
}
public String Initials(){
String f = getFirstName();
char fChar = f.charAt(0);
String m = getMiddleName();
char mChar = m.charAt(0);
String l = getLastName();
char lChar = l.charAt(0);
String s = getSuffixName();
char sChar = s.charAt(0);
String initial = " ";
if(getNumberOfNames() == 0){
initial = " ";
}else if(getNumberOfNames() == 1){
initial = fChar + ".";
}else if(getNumberOfNames() == 2){
initial = fChar + "." + mChar + ".";
}else if(getNumberOfNames() == 3){
initial = fChar + "." + mChar + "." + lChar + ".";
}
//String initial = fChar + "." + mChar + "." + lChar ;
return initial;
}
}