检查列表是否存在数据然后再次输入值

Check List if data exists then input value again

我的主要目标是确定输入的号码是否在列表中不重复,否则用户需要更新新号码。系统会不断要求用户输入不重复的数字。

我目前正在努力获取逻辑以检查我的列表是否包含重复 ID。如果有人输入了重复的 ID,则会提示他重新输入一个新号码。将再次检查新数字,直到系统满足没有重复元素为止。下面的函数returns一个整数,会在main方法中添加到Course类型的List中。

以下是我的函数片段:

public static int ifExist(List<Course> courselist, Iterator<Course> itr,  int personid) {
        
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        boolean found = false;
        boolean flag = false;
        int personid2 = personid;
        String value = null;
        
        while (itr.hasNext()) {
                Course courseItr = itr.next();
                if(courseItr.getPersonID() == personid) {
                    found = true;
                    flag = true;
                    
                    while(found == true) {
                        System.out.print("No duplicate number is accepted. Please enter another number: ");
                        do {
                            // must be a digit from 1 - 10,000
                            String digit = "\d{1,10000}";
                            value = input.nextLine();
                            flag = value.matches(digit);
                            if (!flag) System.out.print("Please a number only!: ");
                        } while (!flag);
                        personid2 = Integer.parseInt(value);
                        
                        if(personid2 != courseItr.getPersonID()) {
                            found= false;
                        }
                    }
                }
        }
        return personid2;
        
    }

执行课程程序时的输出如下所示。请注意,输入 1 表示添加课程列表。

Please select your choice: 1

Enter the person ID: 1
Enter the person name: Alysa
Enter the title of the course: Maths
Enter the year of joining: 2021
Enter the fee of the course: 20.50
New Course has successfully been added.

Please select your choice: 1

Enter the person ID: 1
No duplicate number is accepted. Please enter another number: 1
No duplicate number is accepted. Please enter another number: 1
No duplicate number is accepted. Please enter another number: 1
No duplicate number is accepted. Please enter another number: 2
Enter the person name: Maria
Enter the title of the course: Biology
Enter the year of joining: 2021
Enter the fee of the course: 25.99
New Course has successfully been added.

Please select your choice: 1

Enter the person ID: 2
No duplicate number is accepted. Please enter another number: 2
No duplicate number is accepted. Please enter another number: 2
No duplicate number is accepted. Please enter another number: 2
No duplicate number is accepted. Please enter another number: 1
Enter the person name: Peter
Enter the title of the course: Chemistry
Enter the year of joining: 2021
Enter the fee of the course: 50.50
New Course has successfully been added.

如上所示,说明我的ifExist方法没有起作用(试图把逻辑弄对)。两人ID相同,如1.

当我尝试显示课程列表时

Please select your choice: 3
Person ID: 1, Name: Alysa, Title: Maths, Year: 2021, Fee: .5.
Person ID: 2, Name: Maria, Title: Biology, Year: 2021, Fee: .99.
Person ID: 1, Name: Peter, Title: Chemistry, Year: 2021, Fee: .5.

我用谷歌搜索了一下,但似乎我要么必须使用 Set 删除任何重复项,要么使用 equals/hashcode()。尽管如此,如果有任何经验丰富的 java 程序员帮助澄清或提供有关如何解决此问题的任何想法,我将不胜感激。

新增方法

public static void addCourse(List<Course> courselist, Course course) {
        //check if the id is the same or not
        
        ListIterator<Course> itr = courselist.listIterator();
        
        try {
            @SuppressWarnings("resource")
            Scanner input = new Scanner(System.in); 
            int personid, year;
            String author, title;
            double fee;
                
            System.out.print("\nEnter the person ID: ");
            personid = input.nextInt();
            personid = ifExist(booklist, itr, personid);
            course.setPersonDd(personid);

...
...
...
courselist.add(new Course(personid, author, title, year, fee));
            System.out.println("New Course has successfully been added.");

} catch {
}
}       

谢谢。期待听取开发者的意见。

此致, 西蒙娜11

关于你的第一种方法

问题出在迭代器上。当用户在2之后输入1时,迭代器已经遍历了ID为1的Course,因此无法检测到重复ID。因此,每次用户输入新数字时,都必须重新开始迭代。

List<Course> courselist 参数未使用。

话虽如此,该程序在逻辑上并未得到优化。 ifExists(,) 方法只适用于搜索具有相同 ID 的课程。至于处理用户输入,应该完全在方法之外完成。

这里是ifExists(,)方法的一个例子

public static boolean ifExists(int id, Iterator<Course> iterator){
    while (iterator.hasNext()) {
        Course next = iterator.next();
        if (next.id == id) return true;
    }
    return false;
}

然后在main方法中,你给用户的消息是基于这个方法返回的值。这是一个例子:

Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
while (ifExists(id)) {
    System.out.println("Duplicated ID! Please try another number.");
    id = scanner.nextInt();
} // If ifExists(id) returns false, continue to the code below to enter personal details

关于你的第二种方法

使用 HashSet 而不是 ListIterator。您可以直接调用 HashSet.contains(Obj) 来检查一个 Course 是否已经存在于集合中,而无需循环遍历项目。尽管 List 也有此方法,但它循环遍历所有项目,这与您正在做的类似。

这是因为 HashSet 按哈希码而不是添加顺序对项目进行排序,但 List 不是。因此,当您调用 contains 方法时,它会查找条目号。 (插入哈希码)。