String[] input = course.split(":");
int credits = Integer.parseInt(input[1]);
Integer term = Integer.parseInt(input[3]);
Course cObject = new Course(input[0],credits,input[2],input[3]);
您的 main 中的上述代码段始终假定course
字符串中包含 abc:def:ghi:jkl 至少 3 个“:”。当字符串没有 3 个“:”时,处理错误情况总是一个好习惯。将您的代码修改为如下所示
String[] input = course.split(":");
if(input.length == 4)
{
int credits = Integer.parseInt(input[1]);
Integer term = Integer.parseInt(input[3]);
Course cObject = new Course(input[0],credits,input[2],input[3]);
}
else
{
//show some error message to user
}