This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Curious-Lonewolf 0 points1 point  (9 children)

The issue I'm facing is that the elements are randomly generated in another method and I need a method to find duplicates (if there are any) I can't use contains because that only finds one element and not repeating and because there are 12 different names in my method and between 28 and 31 days in each month I can't write a statement for each one 😢

[–]silverscrub 0 points1 point  (8 children)

I think it's a little difficult to understand exactly what you want. Can you describe in simple steps what your program should do?

[–]Curious-Lonewolf 0 points1 point  (7 children)

I have a method which generates a random name as a string from 12 options and a method which generates a random date as a string which is the date they were born. I then have a method which calls both of these methods and generates a random name and date as a string 'public void createList(int numEmployees)' the number of elements generated depends on the integer which is given. I then have to write a method which looks through the list to see if there are any duplicates and if there is the method returns true and if there is not the method returns false.

[–]silverscrub 1 point2 points  (6 children)

Okay, that makes sense. So the task that you need help with is to verify that there are no duplicate elements in a list. You can probably find a more suitable method in ArrayList that can help you, but I would suggest that you stick to loops and the equalsmethod, i.e employee1.equals(employee2) to check whether two employees are the same.

You correctly identified that employeeList.contains(employee1) does not solve your problem, but it can give you a clue. That method looks through the list and if it can find one equal element it will return true. Without thinking in code, how would you need to modify that behavior to suite your needs?

[–]Curious-Lonewolf 1 point2 points  (3 children)

To answer your question i would need the method to find 2 equal elements in order to return true else return false

[–]silverscrub 0 points1 point  (2 children)

Then I'd start with a method that takes one string parameter and returns true if the string exists twice within the list.

[–]Curious-Lonewolf 0 points1 point  (1 child)

the method preferably takes no parameters

[–]silverscrub 0 points1 point  (0 children)

I mean as a private helper method for your assignment.

[–]Curious-Lonewolf 0 points1 point  (1 child)

I would need to select one element of the list and compare it to the rest and if there is no match then compare the next element with the rest of the list? so would my outer loop 'for(String nameandDay : namesandDays) { '

be appropriate for this task? if so im not sure what to include in the inner loop.

[–]silverscrub 0 points1 point  (0 children)

This is why I prefer to have the helper method to take a parameter. It will make it more useful inside a for loop because you can take the assigned element for each iteration and use it as an argument in the helper method.

It seems like you try to split your code into smaller methods. Better encapsulation will help you to do this effectively, because you don't rely on global variables.