错误是字符串索引超出范围:0。老实说,我甚至不知道我正在做的事情是否正确。
我必须创建一个wholeName方法和一个初始方法。他们的名字不言而喻,几乎整个名称根据用户给我的内容打印出名称。如果他们只给了我一个名字,那就是我要回来的全部。
缩写是相同的。它从名称输入中获取第一个字母,仅返回首字母。像约翰李史密斯将是J.L.S。
任何人都可以帮我理解错误吗?如果你不能说我只是一个初学者。
我有一个返回错误的测试程序 java.lang.StringIndexoutofboundexception:字符串索引超出范围: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;
}
}
答案 0 :(得分: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,则假设它是firstname。确保这始终有效。如果没有,那么你会收到错误。
答案 1 :(得分:0)
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。 尝试重新思考逻辑并重构代码
祝你好运:)