gpt4 book ai didi

java - 我不断收到字符串越界错误,有人可以帮助修复我的代码吗?

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:00 26 4
gpt4 key购买 nike

错误是字符串索引超出范围:0。老实说,我什至不知道我所做的事情是否正确。

我必须创建一个 WholeName 方法和一个初始方法。他们的名字不言自明,几乎 fullName 会根据用户给我的内容打印出名字。如果他们只给我名字和姓氏,那么我就会返回这些。

首字母缩写是相同的。它从输入的姓名中获取第一个字母,并且仅返回首字母缩写。就像约翰·李·史密斯会是 J.L.S.

任何人都可以帮助我理解出了什么问题吗?如果你看不出我只是一个初学者。

我有一个返回错误的测试程序java.lang.StringIndexoutofboundException:字符串索引超出范围:0在 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;
}
}

最佳答案

调用该函数时,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,则假设它是名字。确保这始终有效。如果不是,那么您将收到错误。

关于java - 我不断收到字符串越界错误,有人可以帮助修复我的代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15915467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com