I Started working on java a few months back now I am stuck in exception handling what I need is, that every StudentId, TeamID must go throw one round of exception check so that if the user enters alphabet in place of integer it should send back to the same place to ask that value again. I am using ArrayList to store data. Also all the get**********() methods is in another class called Student. studentList is my variable to access my ArrayList
please help me to find where is my error by fixing it so that my concept will get more clarity. I am stuck here
/***
* A method to check if the same Student ID exists in the ArrayList previously
* @param StudentId
* @return
*/
public static boolean checkStudent(int StudentId) {
for (Student stud : studentList) {
if(stud.getStudentId() == StudentId) {
return true;
}
}
return false;
}
/***
* Method to add student in the ArrayList
* @param scn
*/
public static void addStudent(Scanner scn) {
System.out.println("Enter student ID");
int studentId;
studentId = scn.nextInt();
**//enter the try catch statement here
//ask the user to write the integer not string and revert to this scanner variable**
if (checkStudent(studentId)) {
System.out.println("Student Id " + studentId + " already exists,Please enter the valid details ");
addStudent(scn);
//break;
}else{
System.out.println("Enter student name:");
String studentName;
studentName = scn.next();
System.out.println("Enter team ID:");
**try{
int teamId;
teamId = scn.nextInt();
//ask the user to write the integer not string and revert to this scanner variable
}catch(NumberFormatException e){
//
}finally{
//do something
}**
System.out.println("Enter team name:");
String teamName;try{
teamName = scn.next();
}
Student std = new Student(studentId, studentName, teamId, teamName);
studentList.add(std);
System.out.println("*******************************************");
System.out.println("student with Student ID" + studentId +" added succesfully");
System.out.println("*******************************************");
}
}
**//enter the try catch statement hereYou really need to make an attempt at this code. I doubt anyone will just write it for you.