如何防止或处理此代码中的 java.lang.ArrayIndexOutOfBoundsException?
How can I prevent or handle java.lang.ArrayIndexOutOfBoundsException in this code?
我正在用数组制作电话簿(我必须使用数组)。当我尝试传递仅包含姓氏和首字母(不是数字)或仅包含姓氏的输入行时,我想抛出 IllegalArgumentException。但是,当我尝试对其进行测试时,却抛出了 ArrayIndexOutOfBoundsException。
这是一些addEntry方法。
@Override
public void addEntry(String line) throws IllegalArgumentException{
int size = entries.length;
String[] newLine = line.split("\s+");
String surname = newLine[0];
String initials = newLine[1];
String number = newLine[2];
if (surname.length()<1 || initials.length()<1 || number.length()<1){
throw new IllegalArgumentException("Please provide a Surname, Initials and a Number");
}
Entry newEntry = new Entry(surname, initials, number);
如果我尝试将此条目传递给方法:arrayDirectory.addEntry("Lara AL");
我收到此错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
指向这里:String number = newLine[2];
如果您将 "Lara AL" 作为输入,newLine
数组将被初始化为
["Lara", "AL"]
并且长度为 2。无需单独检查姓氏、首字母和数字的长度,您可以在初始化后检查数组的长度是否小于 3。
String[] newLine = line.split("\s+");
if (newLine.length < 3) {
throw new IllegalArgumentException("Please provide a Surname, Initials and a Number");
}
我觉得你应该这样写
String surname = newLine.length > 0 ? newLine[0] : "";
String initials = newLine.length > 1 ? newLine[1] : "";
String number = newLine.length > 2 ? newLine[2] : "";
在赋值给变量之前检查数组的长度。喜欢:
@Override
public void addEntry(String line) throws IllegalArgumentException{
int size = entries.length;
String[] newLine = line.split("\s+");
if(newLine.length < 3)throw new IllegalArgumentException("Please provide a Surname, Initials and a Number");
String surname = newLine[0];
String initials = newLine[1];
String number = newLine[2];
if (surname.length()< 5 || initials.length()< 5 || number.length()< 5){
throw new IllegalArgumentException("Please provide a Surname, Initials and a Number that is atleast 5 char long");
//do other validations here like number - is it a number or maybe has dashes and spaces
}
Entry newEntry = new Entry(surname, initials, number);
问题正在发生,因为
String[] newLine = line.split("\s+");
当
String line = "Lara AL";
计算结果为:
String[] newLine = ["Lara", "AL"];
当 newLine 仅包含 2 个元素时,您正在尝试访问 newLine[2]
。
这正在发生,因为 \s+
模式匹配一个或多个白色 spaces。
为了避免这种情况,您可以简单地检查 newLine.size() > 2
或将您的正则表达式调整为 \s{1}
或简单地 " "
,这将强制拆分为单个白色 space 及以上将导致:
String[] newLine = ["Lara", " ", "AL"];
我正在用数组制作电话簿(我必须使用数组)。当我尝试传递仅包含姓氏和首字母(不是数字)或仅包含姓氏的输入行时,我想抛出 IllegalArgumentException。但是,当我尝试对其进行测试时,却抛出了 ArrayIndexOutOfBoundsException。
这是一些addEntry方法。
@Override
public void addEntry(String line) throws IllegalArgumentException{
int size = entries.length;
String[] newLine = line.split("\s+");
String surname = newLine[0];
String initials = newLine[1];
String number = newLine[2];
if (surname.length()<1 || initials.length()<1 || number.length()<1){
throw new IllegalArgumentException("Please provide a Surname, Initials and a Number");
}
Entry newEntry = new Entry(surname, initials, number);
如果我尝试将此条目传递给方法:arrayDirectory.addEntry("Lara AL");
我收到此错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
指向这里:String number = newLine[2];
如果您将 "Lara AL" 作为输入,newLine
数组将被初始化为
["Lara", "AL"]
并且长度为 2。无需单独检查姓氏、首字母和数字的长度,您可以在初始化后检查数组的长度是否小于 3。
String[] newLine = line.split("\s+");
if (newLine.length < 3) {
throw new IllegalArgumentException("Please provide a Surname, Initials and a Number");
}
我觉得你应该这样写
String surname = newLine.length > 0 ? newLine[0] : "";
String initials = newLine.length > 1 ? newLine[1] : "";
String number = newLine.length > 2 ? newLine[2] : "";
在赋值给变量之前检查数组的长度。喜欢:
@Override
public void addEntry(String line) throws IllegalArgumentException{
int size = entries.length;
String[] newLine = line.split("\s+");
if(newLine.length < 3)throw new IllegalArgumentException("Please provide a Surname, Initials and a Number");
String surname = newLine[0];
String initials = newLine[1];
String number = newLine[2];
if (surname.length()< 5 || initials.length()< 5 || number.length()< 5){
throw new IllegalArgumentException("Please provide a Surname, Initials and a Number that is atleast 5 char long");
//do other validations here like number - is it a number or maybe has dashes and spaces
}
Entry newEntry = new Entry(surname, initials, number);
问题正在发生,因为
String[] newLine = line.split("\s+");
当
String line = "Lara AL";
计算结果为:
String[] newLine = ["Lara", "AL"];
当 newLine 仅包含 2 个元素时,您正在尝试访问 newLine[2]
。
这正在发生,因为 \s+
模式匹配一个或多个白色 spaces。
为了避免这种情况,您可以简单地检查 newLine.size() > 2
或将您的正则表达式调整为 \s{1}
或简单地 " "
,这将强制拆分为单个白色 space 及以上将导致:
String[] newLine = ["Lara", " ", "AL"];