1

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("*******************************************");   
   }
}  
3
  • **//enter the try catch statement here You really need to make an attempt at this code. I doubt anyone will just write it for you. Commented Sep 20, 2018 at 16:05
  • I made some try but I am not getting this everytime I am getting stackOverflowError Commented Sep 20, 2018 at 16:41
  • @AbhinandanDutt : I've further simplified my answer, hope it helps. Commented Sep 20, 2018 at 17:35

2 Answers 2

2

I'll give you another hint by using a while-loop, you can further implement this logic as per your requirement. You can use this before you call checkStudent()

System.out.println("Enter student ID");
while (true) {
    try {
        int studId= scn.nextInt();
        // terminate loop if everything is ok  
        break;
    } catch (NumberFormatException e) {
        // on an exception, loop still continues
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nicholas K it helped a lot. Thanks for a kick start.
@AbhinandanDutt: I believe you can accept it now. You will get a +2 as well!
1

Please read on the try..catch statement in the Java language specification, or any tutorial (you can find on many by typing "java try catch" into your favorite search engine).

I'm sure you can figure it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.