我已经尝试过这么多代码的变体,我似乎无法正确理解它。 如果我只尝试两个部分,我会得到第二个if语句的数组越界问题。但是,如果名称中有3个部分,它可以正常工作。
import java.util.Scanner;
public class testArray
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
fullName = fullName + " " + " ";
System.out.println(fullName);
String [] parts = fullName.split(" ");
String firstName = parts[0];
String middleName = parts[1];
String lastName = parts[2];
String firstNameInitial = firstName.substring(0,1).toUpperCase(); //capitalizes first initial
String middleNameInitial = middleName.substring(0,1).toUpperCase(); //capitalizes second initial
String lastNameInitial = lastName.substring(0,1).toUpperCase(); //capitalizes third initial
String initials = firstNameInitial + middleNameInitial + lastNameInitial; //Combines initials of name in capital form
if (parts.length == 3)
{
System.out.println(initials);
System.out.println(lastName.toUpperCase() + ", " + firstNameInitial+firstName.substring(1,parts[0].length()) + " " + lastNameInitial + ".");
System.out.println(lastNameInitial + lastName.substring(1,parts[2].length()) + ", " + firstNameInitial+firstName.substring(1,parts[0].length()) + " " + middleNameInitial + middleName.substring(1,parts[1].length()));
}
if (parts.length == 2)
{
System.out.println("error");
}
}
}
答案 0 :(得分:1)
这是因为您初始化变量的方式:
String firstName = parts[0];
String middleName = parts[1];
String lastName = parts[2];
这里,如果部分只有索引0和1(长度为2),你将得到一个例外,因为没有parts[2]
(它的索引超出范围)。
将其修改为以下内容:
String firstName = parts[0];
String middleName = null;
String lastName = parts[1];
if (parts.length > 2) {
middleName = parts[1];
lastName = parts[2];
}
使用此代码,仅当部件的长度大于2时才设置middleName,这意味着索引2将存在。否则,middleName将为null(或者您可以将其更改为空字符串或任何您想要的内容)
答案 1 :(得分:1)
在检查数组的长度之前,您正尝试访问数组中的第3个对象。
试试这个:
import java.util.Scanner;
public class testArray
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
fullName = fullName + " " + " ";
System.out.println(fullName);
String [] parts = fullName.split(" ");
if(parts.length == 3)
{
String firstName = parts[0];
String middleName = parts[1];
String lastName = parts[2];
String firstNameInitial = firstName.substring(0,1).toUpperCase(); //capitalizes first initial
String middleNameInitial = middleName.substring(0,1).toUpperCase(); //capitalizes second initial
String lastNameInitial = lastName.substring(0,1).toUpperCase(); //capitalizes third initial
String initials = firstNameInitial + middleNameInitial + lastNameInitial; //Combines initials of name in capital form
System.out.println(initials);
System.out.println(lastName.toUpperCase() + ", " + firstNameInitial+firstName.substring(1,parts[0].length()) + " " + lastNameInitial + ".");
System.out.println(lastNameInitial + lastName.substring(1,parts[2].length()) + ", " + firstNameInitial+firstName.substring(1,parts[0].length()) + " " + middleNameInitial + middleName.substring(1,parts[1].length()));
}else{
System.out.println("error");
}
}
}
答案 2 :(得分:0)
错误来自:
Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
fullName = fullName + " " + " ";
System.out.println(fullName);
String [] parts = fullName.split(" ");
String firstName = parts[0];
String middleName = parts[1];
String lastName = parts[2];
如果有人输入的文字少于3个字,则会得到ArrayIndexOutOfBoundsException
。你必须添加如下内容:
Scanner input = new Scanner(System.in);
String [] parts = null;
while (input.hasNext()) {
System.out.println("Please enter first middle and last name");
String fullName = input.nextLine();
fullName = fullName + " " + " ";
System.out.println(fullName);
parts = fullName.split(" ");
if (parts.length == 3)
break;
}
String firstName = parts[0];
String middleName = parts[1];
String lastName = parts[2];
这将提示用户输入三个单词,直到您获得正确的金额。
答案 3 :(得分:0)
我认为你想在调用split之后放入这些行:
String [] parts = fullName.split(" ");
if (parts.length == 2)
{
System.out.println("error");
return;
}
所以代码看起来像:
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
fullName = fullName + " " + " ";
System.out.println(fullName);
String [] parts = fullName.split(" ");
if (parts.length == 2)
{
System.out.println("error");
} else if(parts.length == 3) {
String firstName = parts[0];
String middleName = parts[1];
String lastName = parts[2];
String firstNameInitial = firstName.substring(0,1).toUpperCase(); //capitalizes first initial
String middleNameInitial = middleName.substring(0,1).toUpperCase(); //capitalizes second initial
String lastNameInitial = lastName.substring(0,1).toUpperCase(); //capitalizes third initial
String initials = firstNameInitial + middleNameInitial + lastNameInitial; //Combines initials of name in capital form
System.out.println(initials);
System.out.println(lastName.toUpperCase() + ", " + firstNameInitial+firstName.substring(1,parts[0].length()) + " " + lastNameInitial + ".");
System.out.println(lastNameInitial + lastName.substring(1,parts[2].length()) + ", " + firstNameInitial+firstName.substring(1,parts[0].length()) + " " + middleNameInitial + middleName.substring(1,parts[1].length()));
}
}
答案 4 :(得分:0)
我可能会建议拆分白色空间而不是“”
所以例如
String [] parts = fullName.split("\\s+");
为什么要添加空格(fullName = fullName + " " + " ";
)
你也可能没有进行零件比较,因为你将首先从数组中获取字符串提取,在拆分后直接进行if检查(if(parts.length == 3)
)
答案 5 :(得分:0)
问题在这里
String middleName = parts[1];
String lastName = parts[2];
您要为fullName fullName = fullName + " " + " ";
附加两个空格,但实际上这些空格不会附加,因为fullName之后没有文本,因此parts
只包含0位置的一个元素。并且您正在调用不可用的零件数组的第一个位置String middleName = parts[1];
所以您看到ArrayIndexOutOfBoundsException
答案 6 :(得分:0)
0,1,2,3,4>>它们被索引到4,但总成分或长度为5。
所以在 firstName.substring(1,份[0]。长度())
这里,“parts [0] .length()”应该替换为“parts [0] .length() - 1”,因为前面的大小增加了1,因此数组超出范围.. < / p>
同样的错误你会在下一行得到。 所以用.length-1 ..替换每个.length。
并删除此行
fullName = fullName +“”+“”;因为它将变量路径的长度增加到5,因此它将在if语句中显示