【问题标题】:Exception handling using ArrayList with java使用 ArrayList 和 java 处理异常
【发布时间】:2018-09-20 16:02:27
【问题描述】:

几个月前我开始使用 java,现在我陷入了异常处理中,我需要的是,每个 StudentId, TeamID 都必须进行一轮异常检查,以便如果用户输入字母代替整数它应该发送回同一个地方再次询问该值。我正在使用 ArrayList 来存储数据。此外,所有 get**********() 方法 都在另一个名为 Student 的类中。 studentList 是我访问 ArrayList

的变量

请通过修复它来帮助我找到我的错误,以便我的概念更加清晰。我被困在这里了

/***
 * 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 here 你真的需要尝试一下这段代码。我怀疑有人会为你写它。
  • 我做了一些尝试,但每次我得到 stackOverflowError 时我都没有得到这个
  • @AbhinandanDutt :我进一步简化了我的答案,希望对您有所帮助。

标签: java arraylist exception-handling


【解决方案1】:

我会通过使用while循环给你另一个提示,你可以根据你的要求进一步实现这个逻辑。您可以在致电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
    }
}

【讨论】:

  • Nicholas K 帮了大忙。感谢您的启动。
  • @AbhinandanDutt:我相信你现在可以接受了。你也会得到+2!
【解决方案2】:

请阅读try..catch statement in the Java language specification 或任何教程(您可以通过在您最喜欢的搜索引擎中输入“java try catch”来找到许多教程)。

我相信你能猜出来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多