关于java数组越界异常错误的捕获

Grooter 2018-04-17 09:53:51
				print("请输入学生信息,如:Grooter 201610098237 男 计算机工程学院 4)");
while(true) {
s.nextLine();
String stuInfo=s.nextLine();
if(stuInfo.equals("end")) {
break;
}
String[] str=null;
try {
//报数组越界异常
str=stuInfo.split(" ");
}catch(Exception e) {
println("输入错误,请重新输入");
}

try {
//也包数组越界异常
if(str.length!=5) {
println("输入错误,请重新输入");
}
}catch(Exception e) {
println("输入错误,请重新输入");
}

Student student=new Student(str[0],str[1],str[2],str[3],str[4]);
student.write();

}


一旦不规则输入,不管是分割还是判断长度都汇报数组越界异常,连判断长度都异常,又捕获不了,敢问各位大神怎么破?
...全文
1908 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2018-04-18
  • 打赏
  • 举报
回复
str=stuInfo.split(" "); 判断一下str的长度是否是5,如果不是5的话,再执行下面这行就会报错越界 Student student=new Student(str[0],str[1],str[2],str[3],str[4]);
Grooter 2018-04-18
  • 打赏
  • 举报
回复
知道了,我下面Student student=new Student(str[0],str[1],str[2],str[3],str[4])语句没处理错误
Grooter 2018-04-18
  • 打赏
  • 举报
回复
什么意思 。。。。。。。。。》?
yjsl__ 2018-04-17
  • 打赏
  • 举报
回复
好像没什么问题
class Test {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("请输入学生信息,如:Grooter 201610098237 男 计算机工程学院 4)");
		while (true) {
			//s.nextLine();
			String stuInfo = s.nextLine();
			if (stuInfo.equals("end")) {
				break;
			}
			String[] str = null;
			try {
				str = stuInfo.split(" ");
				System.out.println(str);
			} catch (Exception e) {
				System.out.println("输入错误,请重新输入1");
			}

			try {
				if (str.length != 5) {
					System.out.println("输入错误,请重新输入2");
				}
			} catch (Exception e) {
				System.out.println("输入错误,请重新输入3");
			}
		}
	}

}

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧