1

错误是字符串索引超出范围: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; 
    }
}
4

2 回答 2

1

调用函数时,firstname 为空;现在在函数中,语句

  String f = getFirstName(); 
  char fChar = f.charAt(0);

被执行。由于 f 为空(firstName =""; 最初已完成),因此 fChar 给出了该异常。

将其更新为:

    String s = getSuffixName();
    char sChar = s.charAt(0);

    String initial = " ";

      if(getNumberOfNames() == 0){
        initial = " ";
    }else if(getNumberOfNames() == 1){
        String f = getFirstName(); 
        char fChar = f.charAt(0);
        initial = fChar + ".";
    }else if(getNumberOfNames() == 2){
         String f = getFirstName(); 
         char fChar = f.charAt(0);
         String m = getMiddleName();
         char mChar = m.charAt(0);
         initial = fChar + "." + mChar + ".";
    }else if(getNumberOfNames() == 3){
         String f = getFirstName(); 
         char fChar = f.charAt(0);
         String m = getMiddleName();
         char mChar = m.charAt(0);
         String l = getLastName();
         char lChar = l.charAt(0);
        initial = fChar + "." + mChar + "." + lChar + ".";
    }

评论:

请注意,在代码中,如果 getNumberOfNames() == 1,则假设它是名字。确保这始终有效。如果没有,那么您将收到错误消息。

于 2013-04-10T01:40:16.277 回答
0

玛纳斯打败了我。但是,我还会检查您的设置方法(即 setFirstName())

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; // FIRST NAME IS SET OUTSIDE OF CONDITION, WHETHER f.length() == 0 OR NOT
}

此外,您的 numberOfNames 计数器有缺陷。如果只设置了中间名,则计数器将递增 1,但将返回 firstName 而不是 middle。尝试重新思考逻辑并重构代码

祝你好运 :)

于 2013-04-10T01:44:09.310 回答